#python 异常测试
try:
a = "异常测试"
print(aa)
#打印所有异常
except BaseException as msg:
print(msg)
#没有异常时执行else
else:
print("没有异常")
不管是否出现异常,finally 都会被执行
try:
print(aa)
except Exception as e:
print(e)
finally:
print("不管是否异常,都会执行")
python 无自增运算符,处理自增 i = i + 1
def main():
i = 0
with open("常用字典.txt", 'r') as f:
while(1):
if f.readline():
print(f.readline().strip())
i = i + 1
print(i)
else:
print('读取结束')
break
#这样执行的结果只会是打印出一半的文件,每一次 readline() 调用指针都会向下移
这样就行了
def main():
i = 0
with open("常用字典.txt", 'r') as f:
while(1):
strs = f.readline().strip()
if strs:
i = i + 1
print(i)
print(strs)
else:
print('读取结束')
break
网友评论