【发布时间】:2018-05-30 12:48:56
【问题描述】:
使用 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 和 subproccess.Popen。无论使用什么命令,都会发生同样的错误:
subprocess.check_output(['['D:\path\to\exe\program.exe', 'argument'])
FileNotFoundError: [WinError 2] The system cannot find the file specified
据我所知,此错误通常发生在命令尝试执行不存在的路径时,但我无法确定为什么会在这些情况下发生。
【问题讨论】:
-
它没有找到二进制文件
echo。如果你想要一个路径解析机制,你应该设置shell=True。 -
@WillemVanOnsem 这似乎解决了问题。谢谢
标签: python python-3.x subprocess