[英]python subprocess dd and stdout
I am using subprocess to create a random file from /dev/random using unix dd. 我正在使用子进程使用unix dd从/ dev / random创建一个随机文件。 Now, if i want the data output of dd to be written to a file instead of stdout.
现在,如果我希望将dd的数据输出写入文件而不是stdout。 so here's the code am using,
所以这是代码正在使用
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()
This doesn't print the dd output to the file and instead prints it in the stdout. 这不会将dd输出输出到文件,而是将其输出到stdout中。 Is this a specific functionality of dd command?
这是dd命令的特定功能吗? Or am i missing some thing about how subprocess works?
还是我缺少有关子流程工作原理的东西?
dd
outputs its debugging information on stderr, not stdout: 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()
The reason nothing gets written in the file is because its written to stderr. 文件中未写入任何内容的原因是因为其已写入stderr。 Redirect stderr and you will get the result.
重定向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()
Also, flush the buffer after writing "executing the time..." 另外,在写完“执行时间...”后刷新缓冲区。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.