[英]how redirect output to the file in subprocess.Popen
我尝试这样的代码将标准输出重定向到文件:
subprocess.Popen('my command', cwd='my path', shell=True, stdout=stdout.txt, stderr=stdout.txt)
但得到错误: NameError: name 'stdout' is not defined
我使用python版本2.5.2
首先打开文件,如果要保留所有输出/错误的记录或使用w
每次覆盖,请使用a
追加:
with open("stdout.txt","a+") as stdout:
subprocess.Popen('my command', cwd='my path', shell=True, stdout=stdout, stderr=stdout)
使用with
将自动关闭您的文件。
将文件描述符提供给stdout 请参阅doc
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.