HOWTO · Python
How to Execute a Windows CMD Command and Get Output in Python
Run Windows CMD commands from Python with os.system(), or capture command output with pipes using the subprocess module.
On this page
To run a Windows CMD command from Python, use os.system() when the command only needs to display its output in the console. It returns a status value, not the command’s output as a Python string. When Python needs to read the output, use the subprocess module with pipes and communicate().
Execute CMD Commands From a Python Script and Get Output Using os.system()
The os.system() function passes a command to the operating system’s command processor. On Windows, a simple example is os.system("cmd /c echo hello"): CMD runs echo hello, and the text appears in the console. The function’s return value describes the command’s status; it does not contain the displayed text.
Use this approach for a straightforward command whose output a person can read. These examples use Windows CMD syntax: dir, cd, and md are command names used there, and all three are CMD built-ins. The name cd is also used in other shells, so these names are not exclusive to Windows. If you need a command’s result inside your Python program, or need to handle its standard output and standard error separately, use subprocess instead.
Avoid building a command string from untrusted input. The command processor may interpret shell metacharacters as syntax, so pass trusted, fixed commands only when using os.system().
Execute CMD Commands From a Python Script and Get Output Using the subprocess Module
Use subprocess.Popen() to start a process with separate pipes for standard output and standard error. Calling communicate() reads both streams and waits for the process to finish, avoiding a deadlock that can occur when one pipe fills while the other is not read. The captured values are bytes, so decode them with the encoding used by the command before processing them as text.
This self-contained example uses CMD on Windows and the current Python interpreter elsewhere, so its capture pattern can be adapted on either platform. It prints the captured line:
import os
import subprocess
import sys
if os.name == "nt":
command = ["cmd", "/c", "echo", "hello"]
else:
command = [sys.executable, "-c", "print('hello')"]
process = subprocess.Popen(
command,
stdout=subprocess.PIPE,
stderr=subprocess.PIPE,
)
stdout, stderr = process.communicate()
print(stdout.decode("utf-8").strip())
hello
On Windows, cmd /c runs the command and then exits; /k runs it but leaves the command processor open, which is usually unsuitable for a script. With the default shell=False, Python starts the named executable directly without launching an additional shell to parse its arguments. When that executable is cmd, however, cmd still parses its command line and interprets CMD syntax; shell=False does not make untrusted values safe. Never put untrusted input into a CMD command line. Prefer invoking a standalone executable directly with an argument list when possible. If the command writes non-UTF-8 text, decode the captured bytes with the encoding it uses. Check process.returncode when the caller needs to distinguish successful completion from failure.
For more details, see the Python subprocess documentation.