- 代码段
try:
f = open('C:\\Users\87987\Desktop\JJLIN.txt','a')
print(f.read())
except OSError as reason:
print('文件出错啦,原因是:'+str(reason))
finally:
f.close()
- Python引入了with语句来自动帮我们调用close()方法
try:
with open('C:\\Users\87987\Desktop\JJLIN.txt','a') as f:
print(f.read())
except OSError as reason:
print('文件出错啦,原因是:'+str(reason))