Pickle
Python 提供了一个叫作 Pickle 的标准模块,通过它你可以将任何纯 Python 对象存储到一个文件中,并在稍后将其取回。这叫作持久地(Persistently)存储对象。
import pickle
shoplistfile = 'shoplist.data'
shoplist = ['apple','mango','carrot']
f = open(shoplistfile,'wb')
pickle.dump(shoplist,f)
f.close()
del shoplist
f = open(shoplistfile,'rb')
storedlist = pickle.load(f)
print(storedlist)
抛出异常
# encoding = UTF-8
class ShortInputException(Exception):
def __init__(self,length,atleast):
Exception.__init__(self)
self.length = length
self.length = length
self.atleast = atleast
try:
text = input ('enter something -->')
if len(text) < 3:
raise ShortInputException(len(text),3)
except EOFError:
print('why did you do an EOF on me')
except ShortInputException as ex:
print(('ShortInputException: The input was' +
'{0} long ,expected at least {1}').format(ex.length,ex.atleast))
else:
print('no exception was raised')