我正在运行以下尝试代码:
try:
paths = file_system_client.get_paths("{0}/{1}/0/{2}/{3}/{4}".format(container_initial_folder, container_second_folder, chronological_date[0], chronological_date[1], chronological_date[2]), recursive=True)
list_of_paths=["abfss://{0}@{1}.dfs.core.windows.net/".format(storage_container_name, storage_account_name)+path.name for path in paths if ".avro" in path.name]
except Exception as e:
if e=="AccountIsDisabled":
pass
else:
print(e)
如果尝试失败,我既不想打印以下错误,如果遇到此错误,我既不想停止程序执行,也可以:
"(AccountIsDisabled) The specified account is disabled.
RequestId:3159a59e-d01f-0091-5f71-2ff884000000
Time:2020-05-21T13:09:03.3540242Z"
我只想天桥它和打印将会发生的任何其他错误/异常(例如TypeError,ValueError等)。
在Python 3中可行吗?
请注意.get_paths()方法属于azure.storage.filedatalake模块,该模块支持将Python与Azure Data Lake直接连接以进行路径提取。
我提供的注释是要确定我要绕过的异常不是内置的异常。
[[Update]在按照提出的附加答案进行排序后,我将代码修改为:
class AccountIsDisabled(Exception):
pass
def create_list_paths(chronological_date,
container_initial_folder="name1",
container_second_folder="name2",
storage_container_name="name3",
storage_account_name="mame4"
):
list_of_paths=list()
paths = file_system_client.get_paths("{0}/{1}/0/{2}/{3}/{4}".format(container_initial_folder, container_second_folder, chronological_date[0], chronological_date[1], chronological_date[2]), recursive=True)
list_of_paths=["abfss://{0}@{1}.dfs.core.windows.net/".format(storage_container_name, storage_account_name)+path.name for path in paths if ".avro" in path.name]
list_of_paths=functools.reduce(operator.iconcat, result, [])
return list_of_paths
try:
raise AccountIsDisabled
list_of_paths=None
executor=ThreadPoolExecutor(max_workers=8)
list_of_paths=[executor.submit(create_list_paths, i, container_initial_folder, container_second_folder, storage_container_name, storage_account_name).result() for i in date_list]
assert len(list_of_paths)!=0 #an assertion error
except AccountIsDisabled:
pass
except (ValueError, TypeError, ArithmeticError, AssertionError, AttributeError) as e:
print(e)
我已添加一个AssertionError,但未捕获。为什么会这样呢?另外,如果我省略raise AccountIsDisabled
我得到以下“ StorageErrorException”,它不被识别为内置异常。实际上,“ StorageErrorException”是我想超越的自定义异常,但它似乎是Python的已知异常。
"StorageErrorException: (AccountIsDisabled) The specified account is disabled.
RequestId:9463114d-701f-0088-1a78-2f783f000000
Time:2020-05-21T14:03:40.3277133Z"
如果我执行此操作:
try:
list_of_paths=None
executor=ThreadPoolExecutor(max_workers=8)
list_of_paths=[executor.submit(create_list_paths, i, container_initial_folder, container_second_folder, storage_container_name, storage_account_name).result() for i in date_list]
assert len(list_of_paths)>0
except StorageErrorException:
pass
except (ValueError, TypeError, ArithmeticError, AssertionError, AttributeError) as e:
print(e)
我仍然收到StorageErrorException
列出[Python 3.Docs]: Compound statements - The try statement。
有几种方法可以实现这一目标。这是一个:
try:
# ...
except StorageErrorException:
pass
except:
print(sys.exc_info()[1])
请注意,except:
是棘手的,因为您可能会默默地处理不应该处理的异常。另一种方法是捕获代码可能显式引发的任何异常。
try:
# ...
except StorageErrorException:
pass
except (SomeException, SomeOtherException, SomeOtherOtherException) as e:
print(e)
浏览[MS.Docs]: filedatalake package和源代码显示,StorageErrorException是您需要处理的代码。
您想比较异常的type
,将条件更改为:
if type(e)==AccountIsDisabled:
示例:
class AccountIsDisabled(Exception):
pass
print("try #1")
try:
raise AccountIsDisabled
except Exception as e:
if type(e)==AccountIsDisabled:
pass
else:
print(e)
print("try #2")
try:
raise Exception('hi', 'there')
except Exception as e:
if type(e)==AccountIsDisabled:
pass
else:
print(e)
输出:
try #1
try #2
('hi', 'there')