FileNotFoundError: [WinError 2] Das System kann die angegebene Datei nicht finden

匿名 (未验证) 提交于 2019-12-03 01:05:01

问题:

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] Das System kann die angegebene Datei nicht finden 

'Das System kann die angegebene Datei nicht finden' is german for: 'The system can't find the specified file'. Since I'm german myself, I'm not having troubles with german error hanling, but I didn't figure out what is wrong here:

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.

What is wrong here? I'm currently using python 3.4.3, if that helps you.

回答1:

echo is not a program that can be executed but it’s a shell command available in the Windows command line interpreter cmd.exe.

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) #                                                                        ^^^^^^^^^^ 


易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!