gpt4 book ai didi

python - 无法使用 python 3.2 运行调用子进程方法

转载 作者:行者123 更新时间:2023-12-01 05:41:59 26 4
gpt4 key购买 nike

我安装了 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

最佳答案

据我所知,问题不在于subprocess——问题是没有名为echo的可执行文件路径:

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

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

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

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

subprocess.check_output(["notepad.exe", "file.txt"])
<小时/>

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

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

这只会启动一个子 shell,要求它立即退出并返回非零状态代码?!?

关于python - 无法使用 python 3.2 运行调用子进程方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17299435/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com