Python: Start new command prompt on Windows and wait for it finish/exit
Upon reading your comment to my previous answer what you need is:
os.system("start /wait cmd /c {command}")
Keep the windows command reference always at hand!
How to open a CMD window using python code
import os
os.system("start cmd.exe")
How to open a command prompt along with a command to run using a python script?
import subprocess
cmd = subprocess.Popen('cmd.exe /K cd /')
#subprocess.Popen('cmd.exe /K netstat') # run cmd commands like netstat,..etc
#subprocess.Popen('cmd.exe /K python') # open cmd in Python live interpreter mode
#subprocess.Popen('cmd.exe /K my_script.py') # run your python script
read more https://docs.python.org/3/library/subprocess.html#subprocess.Popen
Open Empty Command Prompt and Stay Open with Python
I found out a few ways to do this. Firstly, there are 2 ways of opening command prompt. One is the normal Windows Command Prompt.The other way is by opening it with python as the input. There are a few ways for both.
For Python as the input:
os.system('cmd.exe /C start "Unique_name" cmd.exe')
The "unique_name" is so that it can be closed with:
os.system('taskkill /F /IM "cmd.exe" /FI "WINDOWTITLE eq Unique_name"')
The following also work:
os.system('cmd /C start cmd')
os.system('cmd /C start')
os.system('start cmd')
For Windows Command Prompt:
os.startfile("C://Windows/System32/cmd.exe")
This can be closed with:
os.system("taskkill /f /im cmd.exe")
BAT file: Open new cmd window and execute a command in there
You may already find your answer because it was some time ago you asked. But I tried to do something similar when coding ror. I wanted to run "rails server" in a new cmd window so I don't have to open a new cmd and then find my path again.
What I found out was to use the K switch like this:
start cmd /k echo Hello, World!
start before "cmd" will open the application in a new window and "/K" will execute "echo Hello, World!" after the new cmd is up.
You can also use the /C switch for something similar.
start cmd /C pause
This will then execute "pause" but close the window when the command is done. In this case after you pressed a button. I found this useful for "rails server", then when I shutdown my dev server I don't have to close the window after.
Python: Executing the windows command in windows command prompt from python script
Unless you want to open a new console window you don't need to run cmd.exe
(%COMSPEC%
) in order to run another Python script as a subprocess:
import sys
from subprocess import check_call
check_call([sys.executable, "C:\\examples\\xml2html.py",
"--dir", "c:\\Temp\\abcd", "c:\\tmp\\results.xml"])
windows Command prompt vanishes after running a python script converted to .exe using pyinstaller
You could use try-except
and input()
function, so that when there is any error, it will wait for the user to interact.
Look at this example -
a = input('Please enter a number: ')
try:
int(a) # Converts into a integer type
except ValueError as v:
# This will run when it cannot be converted or if there is any error
print(v) # Shows the error
input() # Waits for user input before closing
For your e.g code, try this -
import sys
try:
x = int(input(" enter a number :"))
y = int(input(" enter a number :"))
except ValueError as v:
print(v)
input('Press any key to exit ')
sys.exit()
if x>100 or y > 100 :
try:
sys.exit("ERROR :value out of range")
except SystemExit as s:
print(s)
input('Press any key to exit ')
sys.exit()
z=x+y
print(z)
You will see that the command prompt does not close immediately
wait for terminal command to finish in python
use communicate:
cmd = subprocess.Popen(command)
cmd.communicate()
# Code to be run after execution finished
Related Topics
Write() Versus Writelines() and Concatenated Strings
Python Library 'Unittest': Generate Multiple Tests Programmatically
Transform "List of Tuples" into a Flat List or a Matrix
Fitting a Histogram with Python
What Is the Correct Syntax for 'Else If'
Nltk-Based Text Processing with Pandas
Str.Startswith with a List of Strings to Test For
How to Format Axis Number Format to Thousands with a Comma in Matplotlib
How to Find the First Key in a Dictionary
I Have Python on My Ubuntu System, But Gcc Can't Find Python.H
Python: How to Remove Empty Lists from a List
How to Change the String Representation of a Python Class
Unbalanced Data and Weighted Cross Entropy
How to Add Default Parameters to Functions When Using Type Hinting