There are three ways to open cmd (command prompt) in Python: os.system(): Execute the command string as a subprocess. subprocess.Popen(): Creates and returns an object representing the subprocess. subprocess.call(): Create and wait for the subprocess to complete, returning the exit code.
Open cmd in Python
To open cmd (command prompt) through Python, you can use the following method :
Method 1: os.system()
<code class="python">import os os.system("cmd")</code>
Method 2: subprocess.Popen()
<code class="python">import subprocess subprocess.Popen("cmd")</code>
Method 3: subprocess.call()
<code class="python">import subprocess subprocess.call("cmd")</code>
Explanation:
os.system()
The function starts with Execute the command string as a subprocess. subprocess.Popen()
The function creates and returns an object representing the subprocess. subprocess.call()
The function creates and waits for the subprocess to complete, and then returns the process exit code. All three methods can open cmd, but there are a few things to note:
cwd
parameter. subprocess.Popen()
and subprocess.call()
allow you to capture the output and errors of a subprocess. The above is the detailed content of How to open cmd in python. For more information, please follow other related articles on the PHP Chinese website!