[英]Python subprocess on win32 not working without with shell=True
看起来很奇怪,但是我试图通过subprocess.check_call()在Python脚本中使用Perl的CPAN。 由于某些原因,它不起作用,并出现以下错误:
WindowsError: [Error 2] The system cannot find the file specified
当我尝试正常运行命令时。
使用shell = True可以解决此问题,但是我已经读到这可能会有一些安全问题。 有人可以向我解释为什么会发生这种情况,并且有办法解决吗?
编辑:示例代码
这是我使用check_call()遍历列表的方式:
packages = {'DateTime':'2','Win32::API':'2','Date::Manip':'2','XML::LibXML':'2','Carp::Assert':'2',
'Digest::CRC':'2','Data::Hexify':'2','Image::ExifTool':'2','File::Mork':'2','DateTime::Format::Strptime':'2',
'Parse::Win32Registry':'2','HTML::Scrubber':'2','Mac::PropertyList':'2','XML::Entities':'2'}
for k,v in packages.iteritems():
try:
res = check_call(shlex.split('cpan install %s' % k), shell=True)
if res == 0:
v = 0
except CalledProcessError:
谢谢!
subprocess.Popen(),重定向了stdout,因此您可以检查输出。
import subprocess as s
import shlex
k = "DateTime"
proc = s.Popen(["cpan", "install", k], stdout=s.PIPE)
output, err = proc.communicate()
print output, err
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.