Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/351.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
subprocess.check_output():在Python中找不到OSError文件_Python - Fatal编程技术网

subprocess.check_output():在Python中找不到OSError文件

subprocess.check_output():在Python中找不到OSError文件,python,Python,执行以下命令及其变体总是会导致错误,我无法理解: 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/python

执行以下命令及其变体总是会导致错误,我无法理解:

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
command=“/bin/dd if=/dev/sda8 count=100 skip=$(expr 19868431049/512)”
打印子流程。检查\u输出([命令])
回溯(最近一次呼叫最后一次):
文件“”,第1行,在
文件“/usr/lib/python2.7/subprocess.py”,第566行,在check_输出中
进程=Popen(stdout=PIPE,*popenargs,**kwargs)
文件“/usr/lib/python2.7/subprocess.py”,第710行,在__
错误读取,错误写入)
文件“/usr/lib/python2.7/subprocess.py”,第1327行,在执行子进程中
引发子对象异常
OSError:[Errno 2]没有这样的文件或目录

它指的是哪个文件?其他命令(如ls、wc)运行正常,该命令在终端上运行良好,但在python脚本上运行不好。

您的
命令是一个包含一个元素的列表。想象一下,如果您试图在shell上运行此命令:

/bin/'dd if='/dev/'sda8 count=100 skip=$(expr 19868431049 '/' 512)'
这就是你正在做的。在您的
bin
目录中几乎肯定没有名为
ddif=
的目录,而在该目录下几乎肯定没有名为
dev
sd8 count=100 skip=$(expr 19868431049
目录,其中包含名为
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 []
尽管这仍然无法移植,因为默认shell是
/bin/sh
,它可能不知道如何处理像
$(…)
(和
expr
)这样的bashism,尽管我认为POSIX要求
expr
作为一个单独的进程存在…。因此:


使用
subprocess.popen

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