我正在尝试使用subprocess模拟以下bash命令:
dpkg --get-selections > a_file.txt
我一直在尝试在Python解释器中进行操作:
>>> args = ['dpkg','--get-selections']
>>> subprocess.call(args, shell=True)
dpkg: error: need an action option
>>> x = subprocess.call(args, shell=True)
dpkg: error: need an action option
>>> args = ['dpkg','--get-selections', '>', 'a_file.txt']
>>> subprocess.call(args, shell=True)
dpkg: error: need an action option
>>> args = ['dpkg','--get-selections', '> a_file.txt']
>>> subprocess.call(args, shell=True)
dpkg: error: need an action option
shell=True
>>> x = subprocess.call(args)
dpkg: no packages found matching > a_file.txt
>>>
关于dpkg: error: need an action option
在子流程中使用它,我似乎无法获得任何具体的信息。
bash命令可以正常工作,但是语法似乎也没有任何问题。
干杯
使用的stdout
参数call()
。另外,您通常不希望shell=True
-在大多数情况下,您不需要在shell中执行它,并且不使用它会更安全(还记得ShellShock吗?)!
args = ['dpkg', '--get-selections']
with open('a_file.txt', 'w') as outfile:
subprocess.call(args, stdout=outfile)
如果您从dpkg本身收到错误,则表示您传递了错误的参数。这与子流程无关。
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句