异常的语句
try :f = open('小甲鱼是天才.txt')
print(f.read())
f.close()
except OSError as reason :
print('文件出错了,原因是:'+ str(reason))
运行结果是:
文件出错了,原因是: No such file or directory: '小甲鱼是天才.txt'
最后一条语句 print('文件出错了,原因是:'+ str(reason)),这里的+str(reason) ,为什么这么写就可以得到 No such file or directory: '小甲鱼是天才.txt'? except OSError as reason :
print('文件出错了,原因是:'+ str(reason))
except OSError as reason,将OSError报错内容赋值给了reason
再打印str(reason) 谢谢大神!
页:
[1]