简体   繁体   中英

Python: Executing the windows command in windows command prompt from python script

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"])

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM