gpt4 book ai didi

Python: "FileNotFoundError"在所有子进程调用上

转载 作者:太空宇宙 更新时间:2023-11-03 11:18:33 26 4
gpt4 key购买 nike

使用 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

据我所知,此错误通常在命令尝试执行不存在的路径时发生,但我无法确定在这些情况下为什么会发生。

最佳答案

您需要包含可执行文件的完整路径。例如,

import subprocess
subprocess.check_output(['/bin/echo', 'hello'])

(虽然不确定它在 Windows 上的确切位置。)

注意:使用 shell=True 也会使其正常工作,但我不建议以这种方式解决它,因为 (1) 您的 PATH 解决方案将有效是一个副作用(你将在运行时对 PATH 的值有一个隐藏的依赖),并且(2)有security concerns .

关于Python: "FileNotFoundError"在所有子进程调用上,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47847084/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com