使用pickle模块存储对象

时间:2022-09-01 08:14:16
import time
import hashlib
import pickle
import os
class Info():
def __init__(self):
self.create_time=time.time() def md5(self):
m=hashlib.md5()
m.update(str(self.create_time).encode('utf-8'))
return m.hexdigest()
def save(self):
if not os.path.exists('md5'):
os.mkdir('md5')
os.chdir('md5')
print(self.md5())
with open(self.md5(),'wb') as f:
pickle.dump(self,f)
@staticmethod
def read():
path=r'C:\Users\Administrator\模块\md5'
res=os.listdir(path)
for item in res:
file_path=r'%s\%s'%(path,item)
# print(file_path)
with open(file_path,'rb') as f:
# print(f)
try:
obj=pickle.load(f)
except Exception as e:
pass
print(obj.create_time) i=Info()
print(i.md5())
i.save()
i.read()

相关文章