Gosia
Gosia

Reputation: 25

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.

Upvotes: 1

Views: 336

Answers (1)

Mert Köklü
Mert Köklü

Reputation: 2231

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)

Upvotes: 3

Related Questions