繁体   English   中英

如何将输出重定向到subprocess.Popen中的文件

[英]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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM