有人能解释一下这里发生了什么吗?即使是内置的cmd.exe命令也无法工作:
Python 3.4.0 (v3.4.0:04f714765c13, Mar 16 2014, 19:25:23) [MSC v.1600 64 bit (AM
D64)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> import subprocess
>>> subprocess.call('dir')
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "D:\Python34\lib\subprocess.py", line 535, in call
with Popen(*popenargs, **kwargs) as p:
File "D:\Python34\lib\subprocess.py", line 848, in __init__
restore_signals, start_new_session)
File "D:\Python34\lib\subprocess.py", line 1104, in _execute_child
startupinfo)
FileNotFoundError: [WinError 2] The system cannot find the file specified
>>>发布于 2014-09-11 18:49:27
要使用'dir',必须传递shell=True
>>> import subprocess
>>> subprocess.call('dir', shell=True)您必须这样做,因为dir是内置在shell中的,它不是一个独立的控制台应用程序。在subprocess.Popen 文档中也提到了这一点。
在使用shell=True的Windows上,COMSPEC环境变量指定默认的shell。在Windows上唯一需要指定shell=True的时候,要执行的命令被内置到(例如dir或)中。您不需要shell=True来运行批处理文件或基于控制台的可执行文件。
https://stackoverflow.com/questions/25794941
复制相似问题