【发布时间】:2013-06-22 08:58:46
【问题描述】:
我安装了 Python 3.2,并且我正在尝试使用 subprocess 模块,但我不断收到错误消息。
我使用的代码是:
import subprocess
subprocess.check_output(["echo", "Hello World!"])
subprocess.check_output("exit 1", shell=True)
我不断收到subprocess.check_output(["echo", "Hello World!"]) 的以下错误
Traceback (most recent call last):
File "<pyshell#13>", line 1, in <module>
subprocess.check_output(["echo", "Hello World!"])
File "C:\Python32\lib\subprocess.py", line 514, in check_output
process = Popen(*popenargs, stdout=PIPE, **kwargs)
File "C:\Python32\lib\subprocess.py", line 744, in __init__
restore_signals, start_new_session)
File "C:\Python32\lib\subprocess.py", line 977, in _execute_child
startupinfo)
WindowsError: [Error 2] The system cannot find the file specified
对于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
【问题讨论】:
-
第二个非常清楚:告诉 shell 以状态 1 退出,并使用
check_output(),它会在非零退出代码时引发异常。
标签: python command-line subprocess