Using dd command through Python's subprocess module

3.9k views Asked by At

Through Python's subprocess module, I'm trying to capture the output of the dd command. Here's the snippet of code:

r = subprocess.check_output(['dd', 'if=/Users/jason/Desktop/test.cpp', 'of=/Users/jason/Desktop/test.out'])

however, when I do something like

print r

I get a blank line.

Is there a way to capture the output of the dd command into some sort of data structure so that I can access it later?

What I essentially want is to have the output below be stored into a list so that I can later do operations on say the number of bytes.

1+0 records in
1+0 records out
4096 bytes transferred in 0.000409 secs (10011579 bytes/sec)
1

There are 1 answers

0
phihag On BEST ANSWER

dd does not output anything to stdout, so your result is correct. However, it does output to stderr. Pass in stderr=subprocess.STDOUT to get the stderr output:

>>> o = subprocess.check_output(
    ['dd', 'if=/etc/resolv.conf', 'of=r'], stderr=subprocess.STDOUT)
>>> print(o)
b'0+1 records in\n0+1 records out\n110 bytes (110 B) copied, 0.00019216 s, 572 kB/s\n'