在 Python 中使用 subprocess.call('dir', shell=True) 时找不到指定的文件 [英] Cannot find the file specified when using subprocess.call('dir', shell=True) in Python
问题描述
在安装了 32 位 python 2.7 的 64 位系统中,我尝试执行以下操作:
导入子流程p = subprocess.call('dir', shell=True)打印
但这给了我:
回溯(最近一次调用最后一次):文件test.py",第 2 行,在 <module> 中p = subprocess.call('dir', shell=True)文件C:Python27libsubprocess.py",第 522 行,调用中返回 Popen(*popenargs, **kwargs).wait()文件C:Python27libsubprocess.py",第 709 行,在 __init__ 中错误读取,错误写入)文件C:Python27libsubprocess.py",第 957 行,在 _execute_child启动信息)WindowsError: [错误 2] 系统找不到指定的文件
如果我在终端做...
目录
...它当然会打印当前文件夹的内容.
我尝试将 shell 参数更改为 shell=False.
实际上我无法使用 subprocess.call()
调用路径上的任何可执行文件.语句 p = subprocess.call('dir', shell=True)
在另一台机器上运行良好,我认为它是相关的.
如果我这样做
subprocess.call('PATH', shell=True)
然后我得到
回溯(最近一次调用最后一次):文件test.py",第 4 行,在 <module> 中subprocess.call('PATH', shell=True)文件C:Python27libsubprocess.py",第 522 行,调用中返回 Popen(*popenargs, **kwargs).wait()文件C:Python27libsubprocess.py",第 709 行,在 __init__ 中错误读取,错误写入)文件C:Python27libsubprocess.py",第 957 行,在 _execute_child启动信息)WindowsError: [错误 2] 系统找不到指定的文件
如果我这样做:
导入操作系统打印 os.curdir
然后我得到
<预><代码>.以上都是在以管理员模式启动的终端中执行的.
我认为您的 COMSPEC
环境变量可能有问题:
我通过深入研究 subprocess.py
并查看回溯指向的 _execute_child
函数发现了这个潜在问题.在那里,您会发现一个以 if shell:
开头的块,它将在环境中搜索所述变量并使用它来创建用于启动进程的参数.
In a 64-bit system with 32 bit python 2.7 installed I am trying to do the following:
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:Python27libsubprocess.py", line 522, in call
return Popen(*popenargs, **kwargs).wait()
File "C:Python27libsubprocess.py", line 709, in __init__
errread, errwrite)
File "C:Python27libsubprocess.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.
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.
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:Python27libsubprocess.py", line 522, in call
return Popen(*popenargs, **kwargs).wait()
File "C:Python27libsubprocess.py", line 709, in __init__
errread, errwrite)
File "C:Python27libsubprocess.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.
I think you may have a problem with your COMSPEC
environment variable:
>>> 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:Python27libsubprocess.py", line 493, in call
return Popen(*popenargs, **kwargs).wait()
File "c:Python27libsubprocess.py", line 679, in __init__
errread, errwrite)
File "c:Python27libsubprocess.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.py
and looking in the _execute_child
function, 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.
这篇关于在 Python 中使用 subprocess.call('dir', shell=True) 时找不到指定的文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!