gpt4 book ai didi

python - 提取 Python subprocess.CalledProcessError 参数列表

转载 作者:行者123 更新时间:2023-12-01 01:18:16 24 4
gpt4 key购买 nike

以下是我们代码库中的代码片段

# global library function
def check_call_noout(params, acceptable_exit_codes = (0,), shellCommand=False):
FNULL = open('/dev/null', 'w')
sts = 1
try:
if shellCommand:
p = subprocess.Popen(params, stdout=FNULL, stderr=FNULL,shell=True)
else:
p = subprocess.Popen(params, stdout=FNULL, stderr=FNULL)
sts = os.waitpid(p.pid, 0)[1]
except:
raise
finally:
FNULL.close()
exit_code = sts >> 8
if exit_code not in acceptable_exit_codes:
raise subprocess.CalledProcessError(exit_code, params)

# driver code
try:
cmd = ["/bin/tar", "--acls", "--selinux", "--xattrs", "-czf a.tar.gz", "./a.xml", "--exclude","\"lost+found\""]
check_call_noout(cmd,(0,1),False)
except subprocess.CalledProcessError as e:
print e.output, e.returncode
except Exception as e:
print(type(e).__name__, e)

我想打印传递到 subprocess.CalledProcessError 对象的 params 参数值,该对象在库函数内部引发并在我的驱动程序代码中捕获。

但是,我无法更改库函数check_call_noout()中的任何内容

最佳答案

如果我理解正确,获取 subprocess.CalledProcessError 类的 __dict__ 属性就可以了:

try:
subprocess.run([...], check=True)
except subprocess.CalledProcessError as e:
print(e.__dict__)

您还可以使用vars函数,该函数将在内部调用__dict__:

try:
subprocess.run([...], check=True)
except subprocess.CalledProcessError as e:
print(vars(e))

关于python - 提取 Python subprocess.CalledProcessError 参数列表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54108696/

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