【发布时间】:2012-02-09 22:19:49
【问题描述】:
伙计们,
我已经辞职来解决这个问题,但我想检查 Python 是否真的按预期运行。
在示例中,“sample.txt”是任何被读取和解析的多行文本文件。
try:
file=open('sample.txt','r')
for line in file:
(some action here)
except:
print "Couldn't open file"
file.close()
我观察到的操作是打开“sample.txt”并处理第一行,然后逻辑进入“except”子句。
WAD 还是这是一个错误?
【问题讨论】:
-
您应该始终捕获特定异常,例如 FileNotFoundException。不要只是包罗万象,否则你会遇到这样的问题,你不知道为什么会失败。
-
for 循环中的确切代码是什么?
标签: python nested-loops