Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using dd command through Python's subprocess module

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)
like image 660
jason adams Avatar asked Mar 15 '23 17:03

jason adams


1 Answers

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'
like image 200
phihag Avatar answered Apr 01 '23 08:04

phihag