Python 2.7 Get命令stdout/stderr

Python 2.7 Get命令stdout/stderr,python,python-2.7,Python,Python 2.7,我正在使用Python 2.7中的子流程模块。我的代码看起来像 cmd = 'unrar l -p%s %s' % (pwd, filename) proc = subprocess.Popen(cmd.split(), stdout=subprocess.PIPE, stderr=subprocess.PIPE, shell=True) (output, err) = proc.communicate() 这段代码在Windows7/10上运行良好,但在Ubuntu 16.04上不起作用。

我正在使用Python 2.7中的子流程模块。我的代码看起来像

cmd = 'unrar l -p%s %s' % (pwd, filename)
proc = subprocess.Popen(cmd.split(), stdout=subprocess.PIPE, stderr=subprocess.PIPE, shell=True)
(output, err) = proc.communicate() 
这段代码在Windows7/10上运行良好,但在Ubuntu 16.04上不起作用。我调试后发现变量err为空,在Windows上包含错误消息。变量输出包含命令的帮助消息。看起来该命令是在没有任何选项的情况下运行的


如果这不是运行命令并获取其stdout/stderr输出的正确方法,那么正确的方法是什么?

感谢Jean-François Fabre提供的提示,我删除了
shell=True
参数,程序运行良好。我想我需要阅读
subprocess.Popen()
上的文档,以了解为什么在Windows上我需要
shell=True
,而在Ubuntu上我不需要,但现在我很高兴

感谢Jean-François Fabre提供的提示,我删除了
shell=True
参数,程序运行良好。我想我需要阅读
subprocess.Popen()
上的文档,以了解为什么在Windows上我需要
shell=True
,而在Ubuntu上我不需要,但现在我很高兴

如果
pwd
filename
包含空格,则会失败。考虑<代码> SLEX.SPLITE()>或>代码> > CMD= [“unrar”,“l”,“-p”,“pWd,文件名”< /COD>,和LUP <代码> shell = true >如果<代码> PWD 或文件名包含空格,它将失败。考虑<代码> SLEX.SPLIT()/代码>或只是<代码> CMD= [“unrar”,“l”,“-p”,“pWd,文件名”< /COD>,和LUP <代码> shell = true