![](/img/trans.png)
[英]How to write to the beginning of a file when using Python subprocess.call()?
[英]How do I redirect stdout to a file when using subprocess.call in python?
我從另一個python腳本(A)調用python腳本(B)。
使用subprocess.call,如何將B的stdout重定向到指定的文件?
我正在使用python 2.6.1。
將文件作為stdout
參數傳遞給subprocess.call
:
with open('out-file.txt', 'w') as f:
subprocess.call(['program'], stdout=f)
替代方法
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
上面的代碼會將命令輸出寫入文件a.txt
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.