I have to execute python script in windows command prompt
I am using the following command to run the command, so that the script opens the command prompt execute it
os.system("start /wait cmd /c {c:\\python27\\python.exe C:\\examples\\xml2html.py --dir c:\\Temp\\abcd c:\\tmp\\results.xml}")
I will be expecting a new directory called "abcd" created at that location and some output files created inside that.
When I run this command normally in the windows command prompt it works. I am not able to execute this in the script. Windows command prompt opens and terminates quickly.
Could any one let me know where exactly is it going wrong with the command please?
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"])
Related
I am running a script and I want the script to start cmd.exe each time it runs. I want to print out information to the user. I need cmd because the script may be compiled as an executable and run as a background task so output may not be available.
I tried this, but I am hoping there's a better way
import os
os.system('start cmd /k echo checking in progress...')
os.system('cmd /k echo checking completed!!!')
The last line didn't print.
I have a python program which prints long outputs. When i try to run that file in vscode, its interactive window isn't enough to view full output. So is there any way to run python file in cmd from VSCODE?
If you are running windows, VSCode uses Powershell as your terminal by default. If you want to use the command prompt instead, hit ctrl+shift+p, type Shell into the command pallet, select Terminal: Select Default Shell, and change it to Command Prompt. I am not sure this will fix your problem as I think Powershell should display just as much output as the CMD, but if you want to try switching terminals, that will do it. Another option is to try to run it natively in CMD or Powershell, rather than using the VSCode integrated terminal. That might be better if changing terminals doesn't help.
As #Jeremiah said, you can also just run your script with the Cmd prompt, without using vs code. Let's say you have the file 'test1.py' saved as C:\Users\bcubrich\Documents\test1.py, and your python env .exe is saved in C:\python27\ArcGIS10.5\python.exe. I just wrote script that had this in it:
print('worked')
Then just input this into the Cmd prompt
C:\python27\ArcGIS10.5\python.exe C:\Users\bcubrich\Documents\test1.py
And it printed
worked
to the console.
More on running python through cmd console here:
http://www.cs.bu.edu/courses/cs108/guides/runpython.html
I'm trying to run the following in a shell. It doesn't work though. The WMIC command has to be run as an administrator. How to I use subprocess.run as an Administrator?
import subprocess
subprocess.run('wmic product where name="Application I Wish To Uninstall" call uninstall', shell=True)
Did you try this?
start your command prompt as administrator
run your python script like "python mypythonapp.py"
I want to launch a python script in a new macOS terminal window from another script. I'm currently using this code:
subprocess.call(['open', '-a', 'Terminal.app', '/usr/bin/python'])
which launches a python prompt in a new terminal window.
But when I try to run a python script with this code:
subprocess.call(['open', '-a', 'Terminal.app', '/usr/bin/python', 'test.py'])
it completely ignores the 'test.py' on the end and starts a python prompt, just like it does without the test.py on the end.
How can I make this work?
Don't use the open -a terminal.app, just use the python executable
subprocess.call(['/usr/bin/python', 'test.py'])
This Python code will open a new terminal window, and then have python3 run test.py:
import os
os.system("""osascript -e 'tell application "Terminal" to do script "python3 test.py"'""")
I'm unable to come up with a way to get this into a subprocess.call() call.
I am able to run the following using os.popen
x = os.popen('dir')
but the following does not work
x = os.popen('cmd /C ""C:\Program Files (x86)\Ixia\Tcl\8.4.14.0\bin\wish84.exe" "C:/Users/lab/Documents/Public/TCL/Scripts/GetStatsFirst2.tcl""')
I have run the exact command on the windows command line, and it manages to open the TCL wish console and does everything. It just doesn't work with os.popen. It is supposed to open a TCL wish console, run the script and close. Is there a way to make this work?
Also, I have tried subprocess.call with this command and it returns 1 and does nothing.
subprocess.call(['cmd', '/C', 'C:\Program Files (x86)\Ixia\Tcl\8.4.14.0\bin\wish84.exe', 'C:/Users/lab/Documents/Public/TCL/Scripts/GetStatsFirst2.tcl'], shell = False)