Search code examples
pythonterminalsubprocesspopendd

Unable to run dd command via popen in python


Code:

from subprocess import PIPE, Popen
op = Popen("lsblk", shell=True, stdout=PIPE).stdout.read()
print op
a = raw_input("Enter Path of Device to be copied : ",)
b = raw_input("Enter new name for Image : ",)
print a+b
op=Popen("dd if="+a+" of="+b+" bs=512").stdout.read()
print op
op = Popen("fls "+b+"|grep "+c, shell=True, stdout=PIPE).stdout.read()
print op
ar = op.split(" ")
d = ar[1]
op = Popen("istat "+b+" "+d, shell=True, stdout=PIPE).stdout.read()
print op
e = raw_input("Enter new filename")
op = Popen("icat "+b+" "+d+" >> "+e, shell=True, stdout=PIPE).stdout.read()
print op
op = Popen("gedit "+e, shell=True, stdout=PIPE).stdout.read()
print op

Error:

Traceback (most recent call last):   
  File "a3.py", line 8, in <module>
    op=Popen("dd if="+a+" of="+b+" bs=512").stdout.read()
  File "/usr/lib64/python2.7/subprocess.py", line 711, in __init__
    errread, errwrite)
  File "/usr/lib64/python2.7/subprocess.py", line 1327, in _execute_child
    raise child_exception OSError: [Errno 2] No such file or directory

Please help, I am not familiar with subprocess.Popen and am a novice in programming.


Solution

  • Popen(["command", "-opt", "value", "arg1", "arg2" ...], stdout=PIPE)
    

    You are not using the Popen function the right way. Here is a short example considering your script :

    op = Popen(["dd", "if="+a, "of="+b, "bs=512"], stdout=PIPE)
    

    You shoud take a look to the subprocess documentation (help(subprocess) in an interpreter)