[英]How to redirect Python's subprocess.call() output to file?
I call an exe tool from Python: 我从Python调用exe工具:
args = '-i' + inputFile
cl = ['info.exe', args]
subprocess.call(cl)
How would I redirect output from the info.exe to out.txt
? 如何将输出从info.exe重定向到
out.txt
?
You can redirect output to file object by specifying it as stdout
argument to subprocess.call
. 您可以重定向输出将它指定为文件对象
stdout
参数subprocess.call
。 Put it in a context manager to write to file safely. 将其放在上下文管理器中以安全地写入文件。
with open('out.txt', 'w') as f:
subprocess.call(cl, stdout=f)
Or open the file in wb mode and use subprocess.check_output
: 或者以wb模式打开文件并使用
subprocess.check_output
:
with open('out.txt', 'wb') as f:
f.write(subprocess.check_output(cl))
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.