Python 列出s3存储桶时windows上的子进程错误

Python 列出s3存储桶时windows上的子进程错误,python,python-2.7,Python,Python 2.7,我试图使用Python 2.7.13列出s3存储桶的内容。这不起作用: >>> args ['aws', 's3', 'ls', 's3://mybucket'] >>> p = subprocess.Popen(args, shell=False, stdout=subprocess.PIPE, stderr=subprocess.PIPE) 错误是: Traceback (most recent call last): File "<stdin

我试图使用Python 2.7.13列出s3存储桶的内容。这不起作用:

>>> args
['aws', 's3', 'ls', 's3://mybucket']
>>> p = subprocess.Popen(args, shell=False, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
错误是:

Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "C:\Python27\lib\subprocess.py", line 711, in __init__
    errread, errwrite)
  File "C:\Python27\lib\subprocess.py", line 959, in _execute_child
    startupinfo)
WindowsError: [Error 2] The system cannot find the file specified

为什么
shell=False
失败,但
shell=True
工作正常?

您完全可以尝试将
子流程
shell=False
一起使用。这是确保可移植性的最佳方式,而且可能启动速度更快

在您的例子中,您的参数看起来不错(没有重定向、没有管道、多个命令):

因此,唯一阻止它使用
shell=False
的是
aws
实际上不是一个可执行文件,而是一个扩展名(此处未显示)包含在
PATHEXT
中的文件

在我的机器上
PATHEXT=.COM;。EXE;。蝙蝠;。CMD;。VBS;。VBE;。JS;。JSE;。WSF;。WSH;。理学硕士;。PY

表示任何
aws.js
aws.bat
。。。文件可以被执行。但是你需要这个外壳

要定位您的程序,请在命令提示符中键入
where aws
,您将获得命令的完整路径和扩展名

如果您不想使用shell=True,那么有一种替代方法也同样适用

args = ['cmd','/c','aws', 's3', 'ls', 's3://mybucket']
因为您已经在运行一个
cmd
,所以不需要
shell=True

['aws', 's3', 'ls', 's3://mybucket']
args = ['cmd','/c','aws', 's3', 'ls', 's3://mybucket']