[英]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.