Search code examples
pythonsubprocessdd

python subprocess dd and stdout


I am using subprocess to create a random file from /dev/random using unix dd. Now, if i want the data output of dd to be written to a file instead of 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. Is this a specific functionality of dd command? Or am i missing some thing about how subprocess works?


Solution

  • dd outputs its debugging information on stderr, not 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()