my_pickle.py----------------------
#!/usr/bin/env python
# encoding: utf-8
# Date: 2018/6/3
import pickle
class MyPickle:
def __init__(self, filename):
self.filename = filename
def dump(self, obj):
print(self.filename)
with open(self.filename, 'wb') as f:
pickle.dump(obj, f)
def load(self):
with open(self.filename, 'rb') as f:
while True:
try:
obj = pickle.load(f)
return obj
except Exception as e:
print(e)
break
settings.py----------------------------
#!/usr/bin/env python
# encoding: utf-8
# Date: 2018/6/16
pickle_example = r'pic_en'
pickle_demo1.py # 应用类------------------------
#!/usr/bin/env python
# encoding: utf-8
# Date: 2018/6/16
# import my_pickle
from my_pickle import MyPickle
from settings import *
a_dict = {'da': 111, 2: [23, 1, 4], '23': {1: 2, 'd': 'dad'}}
my_exm = MyPickle(pickle_example)
my_exm.dump(a_dict)
m_dic = my_exm.load()
print(m_dic)
# {'da': 111, 2: [23, 1, 4], '23': {1: 2, 'd': 'dad'}}