CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutSign UpSign In
jackfrued

CoCalc provides the best real-time collaborative environment for Jupyter Notebooks, LaTeX documents, and SageMath, scalable from individual users to large groups and classes!

GitHub Repository: jackfrued/Python-100-Days
Path: blob/master/Day01-15/code/Day11/ex3.py
Views: 729
1
"""
2
异常机制 - 处理程序在运行时可能发生的状态
3
4
Version: 0.1
5
Author: 骆昊
6
Date: 2018-03-13
7
"""
8
9
import time
10
import sys
11
12
filename = input('请输入文件名: ')
13
try:
14
with open(filename) as f:
15
lines = f.readlines()
16
except FileNotFoundError as msg:
17
print('无法打开文件:', filename)
18
print(msg)
19
except UnicodeDecodeError as msg:
20
print('非文本文件无法解码')
21
sys.exit()
22
else:
23
for line in lines:
24
print(line.rstrip())
25
time.sleep(0.5)
26
finally:
27
# 此处最适合做善后工作
28
print('不管发生什么我都会执行')
29
30