'''
try :
可能出错的代码
except 出现的错误(也可因不写:表示捕获所有异常)
如果出错了怎么办
finally:(可写可不写)
无论try起作用还是except起作用,finally里\
面的语句都会起作用
'''
try:
with open(r"C:\Users\Administrator.USER-20200912KS\Desktop\111.txt","r",encoding="utf-8") as f :
nr = f.read()
except :
with open(r"C:\Users\Administrator.USER-20200912KS\Desktop\111.txt", "r",
encoding="GBK") as f:
nr = f.read()
finally:
print("读取成功!")
try异常捕获
最新推荐文章于 2023-07-09 16:51:24 发布