文件未找到错误:[WinError 2]Das系统无法更改日期

2024-05-04 19:25:18 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在学习如何使用模块subprocess,我刚开始读我的新书。立刻,我收到了一条我不明白的错误信息。

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系统kann die angegebene Datei nicht finden”是德语,用于: '系统找不到指定的文件'。既然我自己是德国人,我对德国人的错误汉林没有意见,但我没有弄清楚这里的问题是什么:

import subprocess

proc = subprocess.Popen(['echo', 'Hello there'], stdout=subprocess.PIPE)
out, err = proc.communicate()
print(out.decode('utf-8'))

在书中,他们说这段代码应该在屏幕上打印“Hello there”,但由于某些原因,它没有

这里怎么了?如果有帮助的话,我目前正在使用python 3.4.3。


Tags: inpyechohellolibstdoutlineproc
1条回答
网友
1楼 · 发布于 2024-05-04 19:25:18

echo不是可以执行的程序,但它是Windows命令行解释器中可用的shell命令cmd.exe

为了执行shell命令,需要将shell=True传递给Popen

proc = subprocess.Popen(['echo', 'Hello there'], stdout=subprocess.PIPE, shell=True)
#                                                                        ^^^^^^^^^^

相关问题 更多 >