在 Python 中使用 subprocess.call('dir', shell=True) 时找不到指定的文件

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/20330385/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-18 20:13:21  来源:igfitidea点击:

Cannot find the file specified when using subprocess.call('dir', shell=True) in Python

pythonshellpython-2.7pathsubprocess

提问by tuple_cat

In a 64-bit system with 32 bit python 2.7 installed I am trying to do the following:

在安装了 32 位 python 2.7 的 64 位系统中,我尝试执行以下操作:

import subprocess
p = subprocess.call('dir', shell=True)
print p

But this gives me:

但这给了我:

Traceback (most recent call last):
  File "test.py", line 2, in <module>
    p = subprocess.call('dir', shell=True)
  File "C:\Python27\lib\subprocess.py", line 522, in call
    return Popen(*popenargs, **kwargs).wait()
  File "C:\Python27\lib\subprocess.py", line 709, in __init__
    errread, errwrite)
  File "C:\Python27\lib\subprocess.py", line 957, in _execute_child
    startupinfo)
  WindowsError: [Error 2] The system cannot find the file specified

If I in the terminal do...

如果我在终端做...

dir

...it of course prints the present folder content.

...它当然会打印当前文件夹的内容。

I have tried to change the shell parameter to shell=False.

我试图将 shell 参数更改为 shell=False。

Edit: Actually I cannot call any executable on the path with subprocess.call(). The statement p = subprocess.call('dir', shell=True)works fine on another machine and I think that it is related.

编辑:实际上我不能用subprocess.call(). 该语句p = subprocess.call('dir', shell=True)在另一台机器上运行良好,我认为它是相关的。

If I do

如果我做

 subprocess.call('PATH', shell=True)

then I get

然后我得到

Traceback (most recent call last):
  File "test.py", line 4, in <module>
    subprocess.call('PATH', shell=True)
  File "C:\Python27\lib\subprocess.py", line 522, in call
     return Popen(*popenargs, **kwargs).wait()
  File "C:\Python27\lib\subprocess.py", line 709, in __init__
    errread, errwrite)
  File "C:\Python27\lib\subprocess.py", line 957, in _execute_child
    startupinfo)
WindowsError: [Error 2] The system cannot find the file specified

If I do:

如果我做:

import os
print os.curdir

then I get

然后我得到

.

All of the above is executed in the terminal started in Administrator mode.

以上都是在以管理员模式启动的终端中执行的。

采纳答案by zigg

I think you may have a problem with your COMSPECenvironment variable:

我认为您的COMSPEC环境变量可能有问题:

>>> import os
>>> os.environ['COMSPEC']
'C:\Windows\system32\cmd.exe'
>>> import subprocess
>>> subprocess.call('dir', shell=True)

    (normal output here)

>>> os.environ['COMSPEC'] = 'C:\nonexistent.exe'
>>> subprocess.call('dir', shell=True)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "c:\Python27\lib\subprocess.py", line 493, in call
    return Popen(*popenargs, **kwargs).wait()
  File "c:\Python27\lib\subprocess.py", line 679, in __init__
    errread, errwrite)
  File "c:\Python27\lib\subprocess.py", line 896, in _execute_child
    startupinfo)
WindowsError: [Error 2] The system cannot find the file specified

I discovered this potential issue by digging into subprocess.pyand looking in the _execute_childfunction, as pointed-to by the traceback. There, you'll find a block starting with if shell:that will search the environment for said variable and use it to create the arguments used to launch the process.

正如回溯所指出的那样,我通过深入研究subprocess.py和查看_execute_child函数发现了这个潜在问题。在那里,您会找到一个以 开头的块if shell:,它将在环境中搜索所述变量并使用它来创建用于启动进程的参数。

回答by aIKid

Before downvote, note that the question was edited after i posted this answer.

在投反对票之前,请注意,在我发布此答案后,该问题已被编辑。

I think os.listdiris more suitable for your case:

我认为os.listdir更适合您的情况:

>>> import os
>>> os.listdir()
['1.txt', '2.txt', '3.txt', 'DLLs', 'Doc', 'e.txt', 'include', 'Lib', 'libs', 'LICENSE.txt', 'm.txt', 'msvcr100.dll', 'NEWS.txt', 'py.exe', 'python.exe', 'python33.dll', 'pythonw.exe', 'pyw.exe', 'README.txt', 'Scripts', 't.txt', 'tcl', 'Tools']

If you want to run it in the command line itself, and just feeling like to call it, you can use os.sytem:

如果您想在命令行本身中运行它,并且只是想调用它,您可以使用os.sytem

os.system('dir')

This will run the commmand, but it returns 0and you can't store it.

这将运行命令,但它返回0并且您无法存储它。

回答by Tom Grundy

In case anyone else besides me doesn't see this in the (3.4) docsright away:

如果除了我之外的其他人没有立即在 (3.4)文档中看到这一点:

On Windows with shell=True, the COMSPEC environment variable specifies the default shell. The only time you need to specify shell=True on Windows is when the command you wish to execute is built into the shell (e.g. dir or copy). You do not need shell=True to run a batch file or console-based executable.

Note Read the Security Considerationssection before using shell=True.

在 shell=True 的 Windows 上,COMSPEC 环境变量指定默认 shell。在 Windows 上,唯一需要指定 shell=True 的时间是当您希望执行的命令内置到 shell 中时(例如 dir 或 copy)。您不需要 shell=True 来运行批处理文件或基于控制台的可执行文件。

注意在使用 shell=True 之前阅读安全注意事项部分。

回答by Malware_656

use Shell= True, It's working for me.

使用 Shell= True,它对我有用。