0% found this document useful (0 votes)
124 views7 pages

Binary File Handling With & Without Pickle - by Rajni Kardam

The document discusses various methods for reading and writing data to binary files in Python, including: 1. Writing and reading text data to a binary file without pickling. 2. Writing lists and dictionaries to a binary file using pickling, and reading the pickled data. 3. Searching for records in a binary file containing pickled lists or dictionaries.

Uploaded by

Himanshu Gupta
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
124 views7 pages

Binary File Handling With & Without Pickle - by Rajni Kardam

The document discusses various methods for reading and writing data to binary files in Python, including: 1. Writing and reading text data to a binary file without pickling. 2. Writing lists and dictionaries to a binary file using pickling, and reading the pickled data. 3. Searching for records in a binary file containing pickled lists or dictionaries.

Uploaded by

Himanshu Gupta
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 7

1.

#Write and read functions in Binary file without pickle ( having text only)

# 1. write in binary file without pickle

f=open('d:\\fl5.bin','wb')

ans='y'

while ans=='y':

nm=input('enter nm:')

nm=nm.encode()

f.write(nm)

ans=input('want to add more data? y or n')

ans=ans.lower()

print('DATA INPUT PROCESS COMPLETED SUCCESSFULLY..')

f.close()

# 2. read in binary file without pickle

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':

d=eval(input('enter the LIST or Dictionary...'))

pickle.dump(d,f)

ans=input('Do you want to add more data..?y or n ')

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()

***************************************************************************

3. #searching records from binary file having LIST or Dictionary

import pickle

f=open('d:\\fl7.bin','rb')

nm=eval(input('enter the value to search:'))

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:

print('record not found.')

*************************************************************************************
4. #searching in a binary file having text only

f=open('d:\\fl5.bin','rb')

data=input('enter data to search:')

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+')

key=input('enter key ')

oldVal=eval(input('enter old value '))

newVal=eval(input('enter new value '))

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)

print('data updated successfully..')

f.seek(tellpointer)

except EOFError:

f.close()

#searching and updating records from binary file having LIST

import pickle

f=open('d:\\fl7.bin','rb+')

#key=input('enter key ')

oldVal=eval(input('enter old value '))

newVal=eval(input('enter new value '))

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)

print('data updated successfully..')

except EOFError:
if found==False:

print('data not found..')

f.close()

#updating records from binary file having text only

f=open('d:\\fl5.bin','rb+')

oldVal=eval(input('enter old value '))

newVal=eval(input('enter new value '))

data=f.read()

data=data.decode()

data=data.replace(oldVal,newVal)

data=data.encode()

f.seek(7)

f.write(data)

print('data updated successfully.....')

f.close()

Updating record in text file-

f=open(r"C:\Users\Comp_Lab_II\Desktop\Varsha.txt","r+")

inputData=input('Enter the data to search..')

newVal=input('Enter New data:')

data=f.read()

newData=data.replace(inputData,newVal)

f.seek(0)

f.write(newData)

f.close()

*************************************************************************************

CSV FILE HANDLING

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':

rn=int(input('ENTER THE ROLL NUMBER:'))

nm=input('ENTER THE NAME:')

L=[rn,nm]

writerObject.writerow (L)

ans=input('more record y or n:')

ans=ans.lower()

print('data written successfully...')

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)

print('data written successfully...')

f.close()

import csv

f=open('D:\\myNewFile.csv',newline='\n')

readObject=csv.reader(f)

for i in readObject:

print(i)
f.close()

You might also like