[英]How do I redirect stdout to a file when using subprocess.call in python?
I'm calling a python script (B) from another python script (A). 我从另一个python脚本(A)调用python脚本(B)。
Using subprocess.call, how do I redirect the stdout of B to a file that specify? 使用subprocess.call,如何将B的stdout重定向到指定的文件?
I'm using python 2.6.1. 我正在使用python 2.6.1。
Pass a file as the stdout
parameter to subprocess.call
: 将文件作为
stdout
参数传递给subprocess.call
:
with open('out-file.txt', 'w') as f:
subprocess.call(['program'], stdout=f)
Alternate approach 替代方法
import subprocess
p=subprocess.Popen('lsblk -l|tee a.txt',stdout=subprocess.PIPE,shell=True)
(output,err)=p.communicate()
p_status=p.wait()
print output
Above code will write output of command to file a.txt 上面的代码会将命令输出写入文件a.txt
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.