Pickle in Python
Master Python with 70+ Hands-on Projects and Get Job-ready - Learn Python
Program 1
#Program for Pickle Serialization and Deserialization
import pickle,os,student
# Program for Serialization
# try:
# f=open("d://mydata/studentdata.txt","wb")
# if(f.writable()):
# rn=int(input("Enter Roll No: "))
# nm=input("Enter Name: ")
# pr=float(input("Enter Percentage: "))
# s=student.Student(rn,nm,pr)
# pickle.dump(s,f) #serialization
# print("File Created........")
# else:
# print("File is read only..")
# except Exception as obj:
# print(obj)
# finally:
# f.close()
# Program for Deserialization
try:
if(os.path.isfile("d://mydata/studentdata.txt")):
f=open("d://mydata/studentdata.txt","rb")
s1=student.Student()
s1=pickle.load(f) #Deserialization
s1.display()
else:
print("File not found")
except Exception as obj:
print(obj)
finally:
f.close()
Your opinion matters
Please write your valuable feedback about DataFlair on Google

