[英]python subprocess dd and stdout
我正在使用子進程使用unix dd從/ dev / random創建一個隨機文件。 現在,如果我希望將dd的數據輸出寫入文件而不是stdout。 所以這是代碼正在使用
import subprocess
out_fd = open('test_file','w')
def os_system_dd():
global out_fd
out_fd.write("executing the time dd command\n")
cmd_list = ['time','dd','if=/dev/random', 'of=/home/anand/sys_entropy_random', 'bs=1M' ,'count=5']
a = subprocess.Popen(cmd_list,stdout=out_fd)
a.wait()
if __name__ == '__main__':
os_system_dd()
這不會將dd輸出輸出到文件,而是將其輸出到stdout中。 這是dd命令的特定功能嗎? 還是我缺少有關子流程工作原理的東西?
dd
在stderr而不是stdout上輸出其調試信息:
import subprocess
out_fd = open('test_file','w')
def os_system_dd():
out_fd.write("executing the time dd command\n")
cmd_list = ['time','dd','if=/dev/random', 'of=/home/anand/sys_entropy_random',
'bs=1M' ,'count=5']
a = subprocess.Popen(cmd_list,stderr=out_fd) # notice stderr
a.communicate()
if __name__ == '__main__':
os_system_dd()
文件中未寫入任何內容的原因是因為其已寫入stderr。 重定向stderr,您將獲得結果。
import subprocess
out_fd = open('test_file','w')
def os_system_dd():
global out_fd
out_fd.write("executing the time dd command\n")
cmd_list = ['date'] #Your list
a = subprocess.Popen(cmd_list,stdout=out_fd, stderr=out_fd)
a.wait()
if __name__ == '__main__':
os_system_dd()
另外,在寫完“執行時間...”后刷新緩沖區。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.