Binary File Handling With & Without Pickle - by Rajni Kardam
Binary File Handling With & Without Pickle - by Rajni Kardam
#Write and read functions in Binary file without pickle ( having text only)
f=open('d:\\fl5.bin','wb')
ans='y'
while ans=='y':
nm=input('enter nm:')
nm=nm.encode()
f.write(nm)
ans=ans.lower()
f.close()
f=open('d:\\fl5.bin','rb')
data=f.read()
data=data.decode()
print(data)
f.close()
************************************************************************************
2. #Write the LIST or Dictionary in binary file and also read it with pickle
import pickle
f=open('d:\\fl7.bin','wb')
ans='y'
while ans=='y':
pickle.dump(d,f)
ans=ans.lower()
print('data entered successfully...')
f.close()
f1=open('d:\\fl7.bin','rb')
try:
while True:
data=pickle.load(f1)
print(data)
except EOFError:
f1.close()
***************************************************************************
import pickle
f=open('d:\\fl7.bin','rb')
found=False
try:
while True:
data=pickle.load(f)
for i in data:
if i==nm:
found=True
except EOFError:
f.close()
if found==True:
print('record found.')
else:
*************************************************************************************
4. #searching in a binary file having text only
f=open('d:\\fl5.bin','rb')
nm=f.read()
nm=nm.decode()
nm=nm.split()
found=False
for i in nm:
if i==data:
found=True
if found==True:
print('data found.')
else:
print('not found')
f.close()
5. #searching and updating records from binary file having LIST or Dictionary
import pickle
f=open('E:\\fl7.bin','rb+')
try:
data={}
while True:
tellpointer=f.tell()
data=pickle.load(f)
if data[key]==oldVal:
data[key]=newVal
f.seek(tellpointer)
pickle.dump(data,f)
f.seek(tellpointer)
except EOFError:
f.close()
import pickle
f=open('d:\\fl7.bin','rb+')
found=False
try:
data=[]
while True:
tellpointer=f.tell()
data=pickle.load(f)
for i in range(len(data)):
if data[i]==oldVal:
found=True
data[i]=newVal
f.seek(tellpointer)
pickle.dump(data,f)
except EOFError:
if found==False:
f.close()
f=open('d:\\fl5.bin','rb+')
data=f.read()
data=data.decode()
data=data.replace(oldVal,newVal)
data=data.encode()
f.seek(7)
f.write(data)
f.close()
f=open(r"C:\Users\Comp_Lab_II\Desktop\Varsha.txt","r+")
data=f.read()
newData=data.replace(inputData,newVal)
f.seek(0)
f.write(newData)
f.close()
*************************************************************************************
import csv
f=open('D:\\myNewFile.csv','w',newline='\n')
writerObject=csv.writer(f)
#print('%s'%'sn',"%20s"%"Name","%20s"%"Date")
#print(50*'=')
writerObject.writerow(['ROLL NO','NAME'])
ans='y'
while ans=='y':
L=[rn,nm]
writerObject.writerow (L)
ans=ans.lower()
f.close()
**************************************************************************
import csv
f=open('D:\\myNewFile.csv',newline='\n')
readObject=csv.reader(f)
writerObject.writerow(['ROLL NO','NAME'])
L2=[[101,'RAM'],[102,'SITA'],[103,'LAXMAN']]
writerObject.writerows(L2)
f.close()
import csv
f=open('D:\\myNewFile.csv',newline='\n')
readObject=csv.reader(f)
for i in readObject:
print(i)
f.close()