Python - 虽然为真:尝试除其他外 - 程序流程问题
Python - While True: Try Except Else - Program Flow Question
我有这个代码使用 While True
和 try catch
。最后的 else 语句总是在 'try' 成功时执行,我想了解原因 - 我想我没有正确理解程序流程。
while True:
try:
subprocess.call(["wget", "-O", "splunk-8.0.1-6db836e2fb9e-Linux-x86_64.tgz", "https://www.splunk.com/bin/splunk/DownloadActivityServlet?architecture=x86_64&platform=linux&version=8.0.1&product=splunk&filename=splunk-8.0.1-6db836e2fb9e-Linux-x86_64.tgz&wget=true"])
print("successfully downloaded splunk enterprise")
time.sleep(2)
except OSError as e:
if e.errno == 2:
print(e)
print("wget doesn't seem to be installed")
time.sleep(2)
print("attempting to install wget")
time.sleep(2)
subprocess.call(["yum", "install", "wget"])
else:
print(e)
print("unknown error response, exiting...")
break
else:
print("something else went wrong while trying to download splunk")
break
如果 try
中的代码 没有 抛出异常,则 try
的 else
子句将执行。如果要捕获任何异常,请使用 except
而不指定异常 class:
except:
print("something else went wrong while trying to download splunk")
break
但是,考虑省略这段代码。正如目前所写的那样,它不会告诉您什么 出错了。如果删除这些行,您将收到一条错误消息,告诉您发生了什么以及错误发生在哪一行。
来自here:
The try … except statement has an optional else clause, which, when present, must follow all except clauses. It is useful for code that must be executed if the try clause does not raise an exception.
因此,如果 try 块中的代码 运行 成功,则 else 子句将 运行。
基于python documentation,try-except可以采用可选的else语句:
The try … except statement has an optional else clause, which, when present, must follow all except clauses. It is useful for code that must be executed if the try clause does not raise an exception.
所以基于此,如果 try 中的代码没有引发任何异常,您的 else 语句将 运行!
你想要的是另一个捕获一般异常的 except 子句,所以你只需要将 else
替换为 except
:
while True:
try:
subprocess.call(["wget", "-O", "splunk-8.0.1-6db836e2fb9e-Linux-x86_64.tgz", "https://www.splunk.com/bin/splunk/DownloadActivityServlet?architecture=x86_64&platform=linux&version=8.0.1&product=splunk&filename=splunk-8.0.1-6db836e2fb9e-Linux-x86_64.tgz&wget=true"])
print("successfully downloaded splunk enterprise")
time.sleep(2)
except OSError as e:
if e.errno == 2:
print(e)
print("wget doesn't seem to be installed")
time.sleep(2)
print("attempting to install wget")
time.sleep(2)
subprocess.call(["yum", "install", "wget"])
else:
print(e)
print("unknown error response, exiting...")
break
except:
print("something else went wrong while trying to download splunk")
break
我有这个代码使用 While True
和 try catch
。最后的 else 语句总是在 'try' 成功时执行,我想了解原因 - 我想我没有正确理解程序流程。
while True:
try:
subprocess.call(["wget", "-O", "splunk-8.0.1-6db836e2fb9e-Linux-x86_64.tgz", "https://www.splunk.com/bin/splunk/DownloadActivityServlet?architecture=x86_64&platform=linux&version=8.0.1&product=splunk&filename=splunk-8.0.1-6db836e2fb9e-Linux-x86_64.tgz&wget=true"])
print("successfully downloaded splunk enterprise")
time.sleep(2)
except OSError as e:
if e.errno == 2:
print(e)
print("wget doesn't seem to be installed")
time.sleep(2)
print("attempting to install wget")
time.sleep(2)
subprocess.call(["yum", "install", "wget"])
else:
print(e)
print("unknown error response, exiting...")
break
else:
print("something else went wrong while trying to download splunk")
break
如果 try
中的代码 没有 抛出异常,则 try
的 else
子句将执行。如果要捕获任何异常,请使用 except
而不指定异常 class:
except:
print("something else went wrong while trying to download splunk")
break
但是,考虑省略这段代码。正如目前所写的那样,它不会告诉您什么 出错了。如果删除这些行,您将收到一条错误消息,告诉您发生了什么以及错误发生在哪一行。
来自here:
The try … except statement has an optional else clause, which, when present, must follow all except clauses. It is useful for code that must be executed if the try clause does not raise an exception.
因此,如果 try 块中的代码 运行 成功,则 else 子句将 运行。
基于python documentation,try-except可以采用可选的else语句:
The try … except statement has an optional else clause, which, when present, must follow all except clauses. It is useful for code that must be executed if the try clause does not raise an exception.
所以基于此,如果 try 中的代码没有引发任何异常,您的 else 语句将 运行!
你想要的是另一个捕获一般异常的 except 子句,所以你只需要将 else
替换为 except
:
while True:
try:
subprocess.call(["wget", "-O", "splunk-8.0.1-6db836e2fb9e-Linux-x86_64.tgz", "https://www.splunk.com/bin/splunk/DownloadActivityServlet?architecture=x86_64&platform=linux&version=8.0.1&product=splunk&filename=splunk-8.0.1-6db836e2fb9e-Linux-x86_64.tgz&wget=true"])
print("successfully downloaded splunk enterprise")
time.sleep(2)
except OSError as e:
if e.errno == 2:
print(e)
print("wget doesn't seem to be installed")
time.sleep(2)
print("attempting to install wget")
time.sleep(2)
subprocess.call(["yum", "install", "wget"])
else:
print(e)
print("unknown error response, exiting...")
break
except:
print("something else went wrong while trying to download splunk")
break