python 09 数据包 异常处理
pickle模块操作文件
pickle.dump(obj, file[, protocol])
序列化对象,并将结果数据流写入到文件对象中。参数protocol是序列化模式,默认值为0,表示以文本的形式序列化。protocol的值还可以是1或2,表示以二进制的形式序列化。
------------------------------------------
pickle.load(file)
读取file
>>> import pickle
>>> my_list = [1,2]
>>> pi_file = open ( 'my_list.pkl','wb') #创建文件,2进制
>>> pickle.dump(my_list,pi_file) #把my_list放入pi_file
>>> pickle.close()
>>> pi_file.close()
>>> pi_file = open ( 'my_list.pkl','rb') #打开文件,2进制
>>> my_list2 = pickle.load(pi_file) #把文件的内容赋予my_list2
>>> print(my_list2)
[1, 2]
常用方法:
数据过大的 集合,列表,字典 可放入一个文件(数据包),然后在代码内部调用
打开文件,显示文件内容
file_name = input ('打开的文件名:')
f = open (file_name)
for e in f:
print(e)
编译器显示
|
Python标准异常总结
|
以下是 Python 内置异常类的层次结构:
BaseException
+-- SystemExit
+-- KeyboardInterrupt
+-- GeneratorExit
+-- Exception
+-- StopIteration
+-- ArithmeticError
| +-- FloatingPointError
| +-- OverflowError
| +-- ZeroDivisionError
+-- AssertionError
+-- AttributeError
+-- BufferError
+-- EOFError
+-- ImportError
+-- LookupError
| +-- IndexError
| +-- KeyError
+-- MemoryError
+-- NameError
| +-- UnboundLocalError
+-- OSError
| +-- BlockingIOError
| +-- ChildProcessError
| +-- ConnectionError
| | +-- BrokenPipeError
| | +-- ConnectionAbortedError
| | +-- ConnectionRefusedError
| | +-- ConnectionResetError
| +-- FileExistsError
| +-- FileNotFoundError
| +-- InterruptedError
| +-- IsADirectoryError
| +-- NotADirectoryError
| +-- PermissionError
| +-- ProcessLookupError
| +-- TimeoutError
+-- ReferenceError
+-- RuntimeError
| +-- NotImplementedError
+-- SyntaxError
| +-- IndentationError
| +-- TabError
+-- SystemError
+-- TypeError
+-- ValueError
| +-- UnicodeError
| +-- UnicodeDecodeError
| +-- UnicodeEncodeError
| +-- UnicodeTranslateError
+-- Warning
+-- DeprecationWarning
+-- PendingDeprecationWarning
+-- RuntimeWarning
+-- SyntaxWarning
+-- UserWarning
+-- FutureWarning
+-- ImportWarning
+-- UnicodeWarning
+-- BytesWarning
+-- ResourceWarning
异常检测
try-except
try:
检测范围
except Exception[as reason]
出现异常(Exception)后的处理代码
else:
其余代码
try:
f = open ('1234567890') #不存在的文件
print(f)
f.close
except OSError: #出错
print('出错')
执行结果
出错
try:
f = open ('1234567890')
print(f)
f.close
except OSError as reason: #错误原因
print('出错' +str(reason ))
执行结果
出错[Errno 2] No such file or directory: '1234567890'
try:
sum = 1+'1'
f = open ('1234567890')
print(f)
f.close
except OSError as reason:
print('文件出错' +str(reason ))
except TypeError as reason:
print('类型出错' +str(reason ))
不推荐
try:
sum = 1+'1'
f = open ('1234567890')
print(f)
f.close
except:
print('出错' )
except OSErro rTypeError: #可放到一起
try-finally
try:
检测范围
except Exception[as reason]
出现异常(Exception)后的处理代码
finally:
无论如何都会被执行的代码
try:
sum = 1+'1'
f = open ('0.txt','w') #创建0
print(f.write('i')) #写入i
sum = 1+'1' #一个错误,当发现错误时,直接跳到except
f.close #没有执行,所以没有写入i
except:
print('出错' )
try:
sum = 1+'1'
f = open ('1234567890','w')
print(f.write('i')
sum = 1+'1'
except:
print('出错' )
finally:
f.close() #一定会执行
raise() 自己触发异常
>>> raise ZeroDivisionError ('!')
Traceback (most recent call last):
File "<pyshell#312>", line 1, in <module>
raise ZeroDivisionError ('!')
ZeroDivisionError: !

浙公网安备 33010602011771号