无法使用Python3.2运行call subprocess方法

2024-04-20 03:21:19 发布

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

我已经安装了python3.2,并且我试图使用subprocess模块,但是我总是遇到错误。在

我使用的代码是:

import subprocess
subprocess.check_output(["echo", "Hello World!"])
subprocess.check_output("exit 1", shell=True)

我一直收到subprocess.check_output(["echo", "Hello World!"])的以下错误

^{pr2}$

对于subprocess.check_output("exit 1", shell=True)行,我得到以下错误:

Traceback (most recent call last):
  File "<pyshell#14>", line 1, in <module>
    subprocess.check_output("exit 1", shell=True)
  File "C:\Python32\lib\subprocess.py", line 521, in check_output
    raise CalledProcessError(retcode, cmd, output=output)
subprocess.CalledProcessError: Command 'exit 1' returned non-zero exit status 1

Tags: 模块inechotruehelloworldoutputcheck
1条回答
网友
1楼 · 发布于 2024-04-20 03:21:19

据我所知,不使用subprocess的问题是在路径上有一个名为echo的可执行文件

WindowsError: [Error 2] The system cannot find the file specified

我的猜测是echo是一个Windows Shell内部命令。尝试使用shell=True启动它:

^{pr2}$

如果要调用可执行程序(“.exe”),则不需要shell。只需传递程序名和可选参数:

subprocess.check_output(["notepad.exe", "file.txt"])

顺便说一句,你想用这条线做什么:

subprocess.check_output("exit 1", shell=True)

这只会启动一个subshell,要求它立即以非零状态代码退出?!?在

相关问题 更多 >