Call a command from Python (Windows version)

Basic

It used to be common to use os.system, but it seems to be modern to use subprocess. Reference: http://stackoverflow.com/questions/4813238/difference-between-subprocess-popen-and-os-system

import subprocess

cmd = 'myprogram myargs'
returncode = subprocess.call(cmd)

Call is sufficient for simple calls, but it seems better to use Popen for more general purposes. According to the following references, Popen has the advantage of not blocking command execution (= advancing to the next line without waiting for the command to finish), and call is an implementation of Popen with a command block. It seems. Reference: http://stackoverflow.com/questions/7681715/whats-the-difference-between-subprocess-popen-and-call-how-can-i-use-them

import subprocess

cmd = 'myprogram myargs'
returncode = subprocess.Popen(cmd)

Caution

However, this does not allow you to call Windows built-in commands such as dir and echo. When I try to call it, I get the error "The specified file cannot be found". Reference: http://stackoverflow.com/questions/19819417/how-to-use-subprocess-in-windows

The solution is to add shell = True.

import subprocess

cmd = 'dir .'
returncode = subprocess.Popen(cmd, shell=True)

As mentioned in the reference material above, if you add shell = True on Windows, the command prompt cmd.exe is actually called and the command is executed on it. If this option is not added or shell = False is specified, the call will not occur unless the actual file name including the extension is specified.

Further attention

Most people probably don't need it, but as mentioned above, since it is the command prompt that is called with shell = True, various shell commands that can be executed on Powershell cannot be called from the subprocess.

Recommended Posts

Call a command from Python (Windows version)
Hit a command in Python (Windows)
Execute command from Python
Call a Python script from Embedded Python in C ++ / C ++
Call CPLEX from Python (DO cplex)
[Python] How to call a c function from python (ctypes)
Call the code generated by Cython from C / C ++ (Windows version)
How to run a Python file at a Windows 10 command prompt
Call C from Python with DragonFFI
Touch a Python object from Elixir
Install xgboost (python version) on Windows
Call popcount from Ruby / Python / C #
python / Make a dict from a list.
Python-dynamically call a function from a string
[2021 version] Python installation Windows 10 (64bit) edition
Call python from nim with Nimpy
Call C / C ++ from Python on Mac
[Python Windows] pip install with Python version
Call c language from python (python.h)
How to get a string from a command line argument in python
Simple code to call a python program from Javascript on EC2
Batch download images from a specific URL with python Modified version
Call dlm from python to run a time-varying coefficient regression model
Everything from building a Python environment to running it on Windows
"Python AI programming" starting from 0 for windows
Send a message from Python to Slack
# 5 [python3] Extract characters from a character string
Building a Python 3.6 environment with Windows + PowerShell
[Python] [Windows] Take a screen capture in Python
Creating a python virtual environment on Windows
Create a deb file from a python package
Generate a class from a string in Python
Use Django from a local Python script
Manipulate BigQuery tables from a Python client
Send a message from Slack to a Python server
Run a Python script from a C # GUI application
Edit Excel from Python to create a PivotTable
How to open a web browser from python
Install Windows 10 from a Linux server with PXE
Create a C array from a Python> Excel sheet
Python on Windows
Simply build a Python 3 execution environment on Windows
Python environment construction (Anaconda + VSCode) @ Windows10 [January 2020 version]
python windows environment
A memorandum of calling Python from Common Lisp
It's easier to iterate over a python file from a command prompt (cmd) .bat
I made a Docker image that can call FBX SDK Python from Node.js
Create a New Todoist Task from Python Script
Python installation (Windows)
[2021 version] From Selenium Basic installation to Web scraping execution by Python Windows 10 (64bit)
How to generate a Python object from JSON
Cross-compile windows version from nim on arch | linux
A note about the python version of python virtualenv
Load and execute command from yml in python
"Python Kit" that calls a Python script from Swift
MeCab from Python
Export Python3 version OpenCV KeyPoint to a file
Call Python library for text normalization from MATLAB
Create a datetime object from a string in Python (Python 3.3)
Run a Python file from html using Django
Trap trapped when running a Python Windows executable