[英]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
但得到错误: NameError: name 'stdout' is not defined
I use python version 2.5.2 我使用python版本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: 首先打开文件,如果要保留所有输出/错误的记录或使用w
每次覆盖,请使用a
追加:
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. 使用with
将自动关闭您的文件。
将文件描述符提供给stdout 请参阅doc
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.