我尝试这样的代码将标准输出重定向到文件:
subprocess.Popen('my command', cwd='my path', shell=True, stdout=stdout.txt, stderr=stdout.txt)
Run Code Online (Sandbox Code Playgroud)
但得到错误: NameError: name 'stdout' is not defined
我使用python版本2.5.2
首先打开文件,a如果要记录所有输出/错误或使用w每次覆盖,请使用追加:
with open("stdout.txt","a+") as stdout:
subprocess.Popen('my command', cwd='my path', shell=True, stdout=stdout, stderr=stdout)
Run Code Online (Sandbox Code Playgroud)
使用with将自动关闭您的文件.
| 归档时间: |
|
| 查看次数: |
3688 次 |
| 最近记录: |