You can start a new command prompt (CMD) on Windows from a Python script and wait for it to finish using the subprocess module. Here's an example:
import subprocess # Command to run in the new CMD window cmd_command = "echo Hello, World!" # Start a new CMD window and wait for it to finish process = subprocess.Popen(["cmd", "/K", cmd_command], shell=True) process.wait() # Check the exit code of the CMD process exit_code = process.returncode print(f"CMD process exited with code {exit_code}") In this example:
We import the subprocess module, which allows us to run external commands.
We define the cmd_command variable, which contains the command you want to execute in the new CMD window. In this case, it's a simple "echo" command.
We use subprocess.Popen() to start a new CMD process. We pass a list with the command and its arguments. The /K option tells CMD to remain open after executing the command, and shell=True allows us to run the command in a shell environment.
We use process.wait() to wait for the CMD process to finish before proceeding with the script.
Finally, we check the exit code of the CMD process using process.returncode. A return code of 0 typically indicates success, while other codes may indicate errors.
You can replace the cmd_command with any command you want to run in the new CMD window.
Python execute command prompt and wait for completion
import subprocess # Execute command prompt command and wait for completion subprocess.run("cmd /c dir", shell=True) Python start new command prompt and wait for it to exit
import subprocess # Start a new command prompt and wait for it to exit subprocess.run("start cmd /c dir", shell=True) Python run command prompt command synchronously
import subprocess # Run command prompt command synchronously subprocess.run("cmd /c dir", shell=True) Python launch command prompt and wait for it to finish
import subprocess # Launch command prompt and wait for it to finish subprocess.run("start cmd /c dir", shell=True) Python execute batch file in a new command prompt and wait
import subprocess # Execute batch file in a new command prompt and wait subprocess.run("start cmd /c myscript.bat", shell=True) Python run command prompt command and wait for it to exit
import subprocess # Run command prompt command and wait for exit subprocess.run("cmd /c dir", shell=True) Python execute command in a new command prompt and wait
import subprocess # Execute command in a new command prompt and wait subprocess.run("start cmd /c echo Hello", shell=True) Python open command prompt and wait for it to complete
import subprocess # Open command prompt and wait for completion subprocess.run("start cmd", shell=True) Python start new command prompt and wait for it to finish
import subprocess # Start new command prompt and wait for finish subprocess.run("start cmd", shell=True) Python run command prompt command and wait for it to complete
import subprocess # Run command prompt command and wait for completion subprocess.run("cmd /c dir", shell=True) nsdictionary angular2-hostbinding keycloak-services grafana cart argmax javasound guice firebase-analytics data-pipeline