在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位系统中,我尝试执行以下操作:
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
但这给了我
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.
我试图将shell参数更改为shell = False.
I have tried to change the shell parameter to shell=False.
实际上,我不能使用subprocess.call()
在路径上调用任何可执行文件.语句p = subprocess.call('dir', shell=True)
在另一台机器上可以正常工作,我认为这是相关的.
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('PATH', shell=True)
然后我得到
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
如果我这样做:
import os
print os.curdir
然后我得到
.
以上所有操作均在以管理员模式启动的终端中执行.
All of the above is executed in the terminal started in Administrator mode.
推荐答案
我认为您的COMSPEC
环境变量可能有问题:
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:\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
我发现了潜在的问题,方法是深入研究subprocess.py
并查看_execute_child
函数,如回溯所指出的那样.在那里,您会找到一个以if shell:
开头的块,它将在环境中搜索所述变量,并使用它来创建用于启动该过程的参数.
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屋!