简体   繁体   中英

how redirect output to the file in subprocess.Popen

I tried such code to redirect standart output to the file:

subprocess.Popen('my command', cwd='my path', shell=True, stdout=stdout.txt, stderr=stdout.txt)

But got error: NameError: name 'stdout' is not defined

I use python version 2.5.2

Open the file first and use a to append if you want to keep a record of all output/errors or use w to overwrite each time:

with open("stdout.txt","a+") as stdout:
   subprocess.Popen('my command', cwd='my path', shell=True, stdout=stdout, stderr=stdout)

Using with will automatically close your file.

将文件描述符提供给stdout 请参阅doc

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