使用 Windows 7、Python 3.5.1:
import subprocess
subprocess.check_output(['echo', 'hello'])
引发错误:
Traceback (most recent call last):
File "<pyshell#8>", line 1, in <module>
subprocess.check_output(['echo', 'hello'])
File "D:\Programs\Python 3.5.1\lib\subprocess.py", line 629, in check_output
**kwargs).stdout
File "D:\Programs\Python 3.5.1\lib\subprocess.py", line 696, in run
with Popen(*popenargs, **kwargs) as process:
File "D:\Programs\Python 3.5.1\lib\subprocess.py", line 950, in __init__
restore_signals, start_new_session)
File "D:\Programs\Python 3.5.1\lib\subprocess.py", line 1220, in _execute_child
startupinfo)
FileNotFoundError: [WinError 2] The system cannot find the file specified
所有子进程调用(例如 subprocess.run、subprocess.call 和 subprocess.Popen)都会发生此错误。无论使用什么命令都会发生相同的错误:
subprocess.check_output(['['D:\path\to\exe\program.exe', 'argument'])
FileNotFoundError: [WinError 2] The system cannot find the file specified
据我所知,当命令尝试执行不存在的路径时通常会发生此错误,但我无法确定为什么会在这些情况下发生。