gpt4 book ai didi

python - python 3.6 subprocess.Popen().returncode of 100 是什么意思?

转载 作者:太空宇宙 更新时间:2023-11-04 09:26:29 25 4
gpt4 key购买 nike

我的 subprocess.Popen() 函数(见下文)返回了 100 returncode。这是什么意思?我找不到显示 100 含义的引用资料。你能给我解释一下吗?谢谢。

import subprocess

def call_subprocess_Popen( cmd, cwd=None ):
''' Execute a command in BASH. kwargs: "cmd" is a list.'''
with subprocess.Popen( cmd, bufsize=1, universal_newlines=True, cwd=cwd,
stdout=subprocess.PIPE, ) as result:
for line in result.stdout:
print( line, end='' )
print( 'result.returncode' )
print( 'result.args' )
if result.returncode != 0:
raise subprocess.CalledProcessError( result.returncode, result.args )
else:
return True

def pkexec_apt_get_y_install( packages ):
print( f'\nProcess {os.getpid()} {threading.current_thread()} pkexec apt_get_y_install ....' )
cmd = [ 'pkexec', 'apt-get', '-y', 'install' ]
cmd.extend( packages )
print( f'cmd = {cmd}' )
if call_subprocess_Popen( cmd ):
return True
else:
return False

apps = [ 'synaptic', 'ubuntu-restricted-extra', 'apt-xapian-index' ]
pkexec_apt_get_y_install( apps )

错误信息:

Process 5979 <_MainThread(MainThread, started 140069990864704)> pkexec apt_get_y_install ....
cmd = ['pkexec', 'apt-get', '-y', 'install', 'synaptic', 'ubuntu-restricted-extra', 'apt-xapian-index']
Reading package lists...
Building dependency tree...
Reading state information...
result.returncode
result.args
Traceback (most recent call last):
File "~/customiseUbuntu1804.py", line 223, in <module>
main()
File "~/customiseUbuntu1804.py", line 205, in main
pkexec_apt_get_y_install( setup_apps )
File "~/customiseUbuntu1804.py", line 76, in pkexec_apt_get_y_install
if call_subprocess_Popen( cmd ):
File "~/customiseUbuntu1804.py", line 47, in call_subprocess_Popen
raise subprocess.CalledProcessError( result.returncode, result.args )
subprocess.CalledProcessError: Command '['pkexec', 'apt-get', '-y', 'install', 'synaptic', 'ubuntu-restricted-extra', 'apt-xapian-index']' returned non-zero exit status 100.

最佳答案

返回代码来自您的外部命令,它可以表示任何意思。

示例:

“test.py”Python 代码:

import subprocess


def call_command():
process = subprocess.Popen(["./test.sh"], stdin=subprocess.PIPE, stdout=subprocess.PIPE)
out, err = process.communicate()
return out, err, process.returncode


output, err, ret_code = call_command()
print("OUT: {}".format(output))
print("ERROR: {}".format(err))
print("Return code: {}".format(ret_code))

“test.sh” Shell 脚本:

#!/usr/bin/env bash

echo "This shell script will return with 100 return code"
exit 100

输出:

>>> python3 test.py 
OUT: b'This shell script will return with 100 return code\n'
ERROR: None
Return code: 100

这意味着你需要在python调用的外部命令中寻找答案。

注意:

也许你有 https 来源。您可以尝试在执行其他 apt-get 命令之前安装 apt-transport-https。命令:apt-get update && apt-get install -y apt-transport-https

关于python - python 3.6 subprocess.Popen().returncode of 100 是什么意思?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57387366/

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