【问题标题】:How can I get success and error code?如何获得成功和错误代码?
【发布时间】:2019-01-06 05:32:02
【问题描述】:

这是我的代码:

import subprocess

def megaGetFile(fileLink,downloadFolder):
            bashCommand = "mega-get --ignore-quota-warn " + fileLink + " " + downloadFolder
            output = subprocess.check_output(['bash','-c', bashCommand])

            print(output)

inecekLink="https://mega.nz/#!KG4FTKjA!ZF5gOE4HPnvo1Ua3kFg5QK5EvZi15enQ4yCRMs2REtA"      
nereye="/home/pi/Desktop/"
inecek="https://mega.nz/#!PC4xRSqA!vYHRQ1RYEagVWAgtQsGK8QRI3AqS_BjfV2ZV-GP9Vgw"

megaGetFile(inecekLink,nereye)

inecekLink 正在下载但返回代码:b' ' inecek 没有下载,因为我删除了它并返回代码:

Traceback (most recent call last):
  File "./megaCMD.py", line 50, in <module>
    megaGetFile(inecek,nereye)
  File "./megaCMD.py", line 36, in megaGetFile
    output = subprocess.check_output(['bash','-c', bashCommand])
  File "/usr/lib/python3.5/subprocess.py", line 316, in check_output
    **kwargs).stdout
  File "/usr/lib/python3.5/subprocess.py", line 398, in run
    output=stdout, stderr=stderr)
subprocess.CalledProcessError: Command '['bash', '-c', 'mega-get --ignore-quota-warn https://mega.nz/#!PC4xRSqA!vYHRQ1RYEagVWAgtQsGK8QRI3AqS_BjfV2ZV-GP9Vgw /home/pi/Desktop/']' returned non-zero exit status 247

如何获取成功和错误代码?

【问题讨论】:

标签: python python-3.x error-handling subprocess


【解决方案1】:

你需要捕获异常,例如:

try:
    output = subprocess.check_output(['bash','-c', bashCommand])
    return_status = 0
except subprocess.CalledProcessError as e:
    output = e.output
    return_status = e.returncode

print(return_status, output)

【讨论】:

  • 那就搞清楚你的​​bashCommand为什么返回非零状态,这和python无关。
  • 我知道您为什么返回非零结果。我想在结果为零时得到它。
  • 对不起,我不明白你的问题,如果结果为零,那么try 块将继续。
猜你喜欢
  • 2013-03-11
  • 1970-01-01
  • 2020-04-03
  • 2014-07-10
  • 1970-01-01
  • 2015-05-06
  • 2010-12-25
  • 1970-01-01
  • 2020-06-11
相关资源
最近更新 更多