FileNotFoundError: [WinError 2] 系统找不到指定的文件 [英] FileNotFoundError: [WinError 2] The system can't find the specified file
问题描述
我目前正在学习如何使用 subprocess
模块,我刚刚开始阅读我的新书.立即,我收到了一条我不明白的错误消息.
I'm currently learning how to use the module subprocess
and I just started with my new book. Instantly, I got an error message which I don't understand.
Traceback (most recent call last):
File "D:/me/Python/subprocess.py", line 3, in <module>
proc = subprocess.Popen(['echo', 'Hello there'], stdout=subprocess.PIPE)
File "C:\Python34\lib\subprocess.py", line 859, in __init__
restore_signals, start_new_session)
File "C:\Python34\lib\subprocess.py", line 1112, in _execute_child
startupinfo)
FileNotFoundError: [WinError 2] The system can't find the specified file
我不知道这里出了什么问题:
import subprocess
proc = subprocess.Popen(['echo', 'Hello there'], stdout=subprocess.PIPE)
out, err = proc.communicate()
print(out.decode('utf-8'))
在书中他们说这段代码应该在屏幕上打印你好",但由于某种原因,它没有.
In the book they said this code should print 'Hello there' on the screen but for some reason, it doesn't.
这里有什么问题?如果对您有帮助,我目前正在使用 python 3.4.3.
What is wrong here? I'm currently using python 3.4.3, if that helps you.
推荐答案
echo
不是可以执行的程序,但它是 Windows 命令行解释器 cmd 中可用的 shell 命令.exe
.
echo
is not a program that can be executed but it’s a shell command available in the Windows command line interpreter cmd.exe
.
为了执行shell命令,你需要将shell=True
传递给Popen
:
In order to execute shell commands, you need to pass shell=True
to Popen
:
proc = subprocess.Popen(['echo', 'Hello there'], stdout=subprocess.PIPE, shell=True)
# ^^^^^^^^^^
这篇关于FileNotFoundError: [WinError 2] 系统找不到指定的文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!