我试图读取符合特定条件的文件夹中的所有文件。一旦我引发异常,我的程序就崩溃了。即使有异常但我仍然试图继续执行。使Python代码异常后继续
这就是我几秒钟后得到的结果。
error <type 'exceptions.IOError'>
这里是我的代码
import os
path = 'Y:\\Files\\'
listing = os.listdir(path)
try:
for infile in listing:
if infile.startswith("ABC"):
fo = open(infile,"r")
for line in fo:
if line.startswith("REVIEW"):
print infile
fo.close()
except:
print "error "+str(IOError)
pass
“我想继续即使有例外” - 从来没有做到这一点,除非你有你的程序应该如何继续的明确计划。假设你的代码在except:block之后依赖于文件内容,在IO错误之后继续是一个坏主意。让异常崩溃程序,以便您可以实际注意到并修复潜在的问题。 –