简体   繁体   中英

How to run windows command line from python script?

I have a script in Python which gives a few files but I also need a log file. Usually I use this command in windows cmd:

py name.py > name.log

This script is part of a project and I need to run it from python. I tried this:

subprocess.call(["py","name.py",">","name.log"])

And it give me all the files that the script prepares but not the log file.

Use os.system

os.system("py name.py > name.log")

Or, you can just pass an open file handle for the stdout argument to subprocess.call :

args = ['name.py']
cmd = ['py'] + args
with open('name.log', "w") as outfile:
    subprocess.call(cmd, stdout=outfile)

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