如果 for 循环中连续出现两个异常,则执行某些操作
Do something if two exceptions in a row occur in a for loop
我正在编写一个 python 脚本,该脚本更改为多个目录并从中解析不同的文件(最终我用解析的数据填充 MySQL 数据库)。实际上我首先尝试更改为 /baseDirA/the/rest/is/the/same
并解析我想要的表。如果 /baseDirA/the/rest/is/the/same
不存在,我尝试更改为 /baseDirB/the/rest/is/the/same
并解析我可以从 /baseDirA/the/rest/is/the/same
.
解析的相同表
在我的代码中,已经太长无法粘贴到这里,我有一个 try except
语句,到目前为止,打印一条消息以防目录(即 /baseDirA/the/rest/is/the/same
或 /baseDirB/the/rest/is/the/same
) 不存在,如下例所示
import os
# define the two baseDirs
dirs = 'baseDirA baseDirB'.split()
for basedir in dirs:
try:
cwd = f"{basedir}/the/rest/is/the/same"
os.chdir(cwd)
# Then I am doing the operations to parse different files
# Below is the except statement, in case one of the directories does not exist
except FileNotFoundError:
print(f"WARNING: directory {cwd} does not exist")
我现在有三种可能的结果
/baseDirA/the/rest/is/the/same
存在:
- 然后我
cd
到这个目录下进行我想要的操作
/baseDirA/the/rest/is/the/same
不存在但 /baseDirB/the/rest/is/the/same
存在:
- 我
cd
到/baseDirB/the/rest/is/the/same
并执行我想要的操作
/baseDirA/the/rest/is/the/same
和 /baseDirB/the/rest/is/the/same
都不存在。
- 在这种情况下,使用我当前的
try except
语句,我会收到如下消息:
WARNING: directory /baseDirA/the/rest/is/the/same does not exist
WARNING: directory /baseDirB/the/rest/is/the/same does not exist
并且,在这种情况下,即如果我连续两次 执行 except
语句 (没有 try
语句中的操作),我想做点别的。
最好的方法是什么?在我举例说明的循环之前添加另一个 for
循环是否最好,只是为了检查两个目录是否存在?或者我可以在 except
语句下面做一些事情吗?
如果目录存在,您可以 break
跳出 for 循环,并添加一个 else
子句,其中的代码将在循环未跳出时执行:
import os
# define the two baseDirs
dirs = 'baseDirA baseDirB'.split()
for basedir in dirs:
try:
cwd = f"{basedir}/the/rest/is/the/same"
os.chdir(cwd)
...
break
except FileNotFoundError:
print(f"WARNING: directory {cwd} does not exist")
else:
print("No directories existed")
我正在编写一个 python 脚本,该脚本更改为多个目录并从中解析不同的文件(最终我用解析的数据填充 MySQL 数据库)。实际上我首先尝试更改为 /baseDirA/the/rest/is/the/same
并解析我想要的表。如果 /baseDirA/the/rest/is/the/same
不存在,我尝试更改为 /baseDirB/the/rest/is/the/same
并解析我可以从 /baseDirA/the/rest/is/the/same
.
在我的代码中,已经太长无法粘贴到这里,我有一个 try except
语句,到目前为止,打印一条消息以防目录(即 /baseDirA/the/rest/is/the/same
或 /baseDirB/the/rest/is/the/same
) 不存在,如下例所示
import os
# define the two baseDirs
dirs = 'baseDirA baseDirB'.split()
for basedir in dirs:
try:
cwd = f"{basedir}/the/rest/is/the/same"
os.chdir(cwd)
# Then I am doing the operations to parse different files
# Below is the except statement, in case one of the directories does not exist
except FileNotFoundError:
print(f"WARNING: directory {cwd} does not exist")
我现在有三种可能的结果
/baseDirA/the/rest/is/the/same
存在:- 然后我
cd
到这个目录下进行我想要的操作
- 然后我
/baseDirA/the/rest/is/the/same
不存在但/baseDirB/the/rest/is/the/same
存在:- 我
cd
到/baseDirB/the/rest/is/the/same
并执行我想要的操作
- 我
/baseDirA/the/rest/is/the/same
和/baseDirB/the/rest/is/the/same
都不存在。- 在这种情况下,使用我当前的
try except
语句,我会收到如下消息:
- 在这种情况下,使用我当前的
WARNING: directory /baseDirA/the/rest/is/the/same does not exist
WARNING: directory /baseDirB/the/rest/is/the/same does not exist
并且,在这种情况下,即如果我连续两次 执行 except
语句 (没有 try
语句中的操作),我想做点别的。
最好的方法是什么?在我举例说明的循环之前添加另一个 for
循环是否最好,只是为了检查两个目录是否存在?或者我可以在 except
语句下面做一些事情吗?
如果目录存在,您可以 break
跳出 for 循环,并添加一个 else
子句,其中的代码将在循环未跳出时执行:
import os
# define the two baseDirs
dirs = 'baseDirA baseDirB'.split()
for basedir in dirs:
try:
cwd = f"{basedir}/the/rest/is/the/same"
os.chdir(cwd)
...
break
except FileNotFoundError:
print(f"WARNING: directory {cwd} does not exist")
else:
print("No directories existed")