3b.file hadling online_ binary
3b.file hadling online_ binary
Creating f=open("file.txt","wb")
Binary f=open("file.dat",“wb")
Files in f=open('file1.xyz',’rb')
Python
f=open('file1.txt’, ‘ab+’)
PICKELING AND UNPICKLING USING
PICKEL MODULE
PICKELING AND UNPICKLING USING
PICKEL MODULE
1) dump() method
2) load() method
pickle.dump() Method
dump(object,fileobject)
load(fileobject)
pickle.dump() Method
pickle.dump() Method
OUTPUT
pickle.dump() Method
import pickle
f=open("file1.txt","wb")
d={1:"apple",2:"mango"}
pickle.dump(d,f) Dump writes the object d in file object f
Writing into Binary files
import pickle
f=open("file1.txt","wb")
a="New thing"
l=[10,20,30,40]
d={1:"apple",2:"mango"}
pickle.dump(a,f)
pickle.dump(l,f)
pickle.dump(d,f)
f.close()
Reading data from a binary file (Unpickling)
• Simple reading
import pickle
f=open("file1.txt","rb")
m=pickle.load(f)
print(m,type(m))
n=pickle.load(f)
print(n,type(n))
o=pickle.load(f) Load method returns object data from the pickled file
print(o,type(o))
How to read data from binary file where no of records are not known
Exception handled
EOFError
Serialization
The pickle module is used for implementing binary protocols for serializing
and de-serializing a Python object structure.
def readdata():
f=open("student.txt","rb")
while(True):
try:
obj=pickle.load(f)
print(obj)
except EOFError:
f.close()
break
3) Implement menu driven