[英]Cannot find the file specified when using subprocess.call('dir', shell=True) in Python
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()
. 编辑:实际上我不能使用
subprocess.call()
在路径上调用任何可执行文件。 The statement p = subprocess.call('dir', shell=True)
works fine on another machine and I think that it is related. 语句
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. 以上所有操作均在以管理员模式启动的终端中执行。
I think you may have a problem with your COMSPEC
environment 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.py
and looking in the _execute_child
function, as pointed-to by the traceback. 我通过钻研发现了这个潜在的问题
subprocess.py
并期待在_execute_child
通过追踪功能,指向的。 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. 在那里,您将找到一个以
if shell:
开头的块,它将在环境中搜索所述变量,并使用其创建用于启动进程的参数。
Before downvote, note that the question was edited after i posted this answer. 在降级投票之前,请注意,在我发布此答案后,问题已被编辑。
I think os.listdir
is 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 0
and you can't store it. 这将运行命令,但返回
0
,您将无法存储它。
In case anyone else besides me doesn't see this in the (3.4) docs right away: 如果除我以外的其他人没有立即在(3.4) 文档中看到此信息:
On Windows with shell=True, the COMSPEC environment variable specifies the default shell.
在shell = True的Windows上,COMSPEC环境变量指定默认的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 (eg dir or copy).
在Windows上唯一需要指定shell = True的时间是将要执行的命令内置到shell中(例如dir或copy)。 You do not need shell=True to run a batch file or console-based executable.
您不需要shell = True即可运行批处理文件或基于控制台的可执行文件。
Note Read the Security Considerations section before using shell=True.
注意在使用shell = True之前,请阅读“ 安全注意事项”部分。
使用Shell = True,它对我有用。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.