Subprocess.check_output (): OSError file not found in Python

Running the following command and its variations always leads to an error that I simply cannot understand:

command = "/bin/dd if=/dev/sda8 count=100 skip=$(expr 19868431049 / 512)"

print subprocess.check_output([command])

Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/usr/lib/python2.7/subprocess.py", line 566, in check_output
    process = Popen(stdout=PIPE, *popenargs, **kwargs)
  File "/usr/lib/python2.7/subprocess.py", line 710, in __init__
    errread, errwrite)
  File "/usr/lib/python2.7/subprocess.py", line 1327, in _execute_child
    raise child_exception
OSError: [Errno 2] No such file or directory

Which file do you mean? other commands, such as ls, wc, work correctly, although the command also works well on the terminal, but not a python script.

+4
source share
2 answers

Yours commandis a list with one item. Imagine if you tried to run this in a shell:

/bin/'dd if='/dev/'sda8 count=100 skip=$(expr 19868431049 '/' 512)'

It is effective what you do. Your directory has binalmost no directory with the name dd if=, and there is almost no directory devunder the directory sd8 count=100 skip=$(expr 19868431049with the program with the name in it 512.

, , , :

command = ['/bin/dd', 'if=/dev/sda8', 'count=100', 'skip=$(expr 19868431049 / 512)']
print subprocess.check_output(command) # notice no []

: $(expr 19868431049 / 512) Python dd; bash. , , Python, bash:

command = ['/bin/dd', 'if=/dev/sda8', 'count=100', 
           'skip={}'.format(19868431049 // 512)]
print subprocess.check_output(command)

, bash , , , shell=True:

command = "/bin/dd if=/dev/sda8 count=100 skip=$(expr 19868431049 / 512)"
print subprocess.check_output(command, shell=True) # still no []

, /bin/sh, , , $(…) ( expr), , , POSIX , expr ...). :

command = "/bin/dd if=/dev/sda8 count=100 skip=$(expr 19868431049 / 512)"
print subprocess.check_output(command, shell=True, executable='/bin/bash')
+12

, subprocess.popen

command = "echo $JAVA_HOME"
proc = subprocess.Popen(command,stdout=subprocess.PIPE,shell=True)
0
source

Source: https://habr.com/ru/post/1584852/


All Articles