import pickle def pickleWrite(fileName,data): f = open(fileName, 'wb') pickle.dump(data, f) f.close() def pickleRead(fileName): f = open(fileName, 'rb') data = pickle.load(f) f.close() return data class Student(object): marks=None def __init__(self,n='name'): self.name=n def setMark(self,m=0): self.marks=m def getMark(self): return self.marks def getName(self): return self.name fiieName='data.pk' s1=Student("jonycse") s1.setMark(5) pickleWrite(fiieName,s1) #object s1 saved on file 'data.pk'
Now pickle object s1 saved on file 'data.pk'. You just need to read the file for getting saved object. I am using two function named pickleWrite and pickleRead for read write data.
#s1=Student("jonycse") #s1.setMark(5) #pickleWrite(fiieName,s1) s2=pickleRead('data.pk') print s2.getName() print s2.getMark()
jonycse 5
0 comments:
Post a Comment