0% found this document useful (0 votes)
37 views

Binary File Using Dictionary

The document contains code to perform CRUD operations on a student dictionary stored as a pickle file. It defines functions to create, display, search by stream, update percent, and delete a record from the dictionary file.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
37 views

Binary File Using Dictionary

The document contains code to perform CRUD operations on a student dictionary stored as a pickle file. It defines functions to create, display, search by stream, update percent, and delete a record from the dictionary file.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 4

#{adm: ____, name: ____, percent: ___, stream: ___}

import pickle
import os

def CREATE():
f=open('Stud_DICT.dat', 'wb')
A=int(input('Enter admission number: '))
N=input('Enter name: ')
P=float(input('Enter percent: '))
if P>=90: S='Science'
elif 75<=P<90: S='Commerce'
else: S='Humanities'
R={'adm': A, 'name': N, 'percent': P, 'stream': S}
pickle.dump(R, f)
f.close()

def DISPLAY():
try:
f=open('Stud_DICT.dat', 'rb')
try:
while True:
R=pickle.load(f)
print(R)
except EOFError:
pass
f.close()
except FileNotFoundError:
print('No such file')
def SEARCH_STREAM():
try:
f=open('Stud_DICT.dat', 'rb')
S=input('Enter stream to search: ')
try:
flag=False
while True:
R=pickle.load(f)
if R['stream'].upper()==S.upper():
flag=True
print(R)
except EOFError:
f.close()
if not flag:
print('Record not found')
except FileNotFoundError:
print('No such file')
def UPDATE_PERCENT():
try:
f=open('Stud_DICT.dat', 'rb+')
pos=0
flag=False
A=int(input('Enter admission number to update percent: '))
try:
while True:
R=pickle.load(f)
if R['adm']==A:
flag=True
R['percent']=float(input('Enter updated percent: '))
f.seek(pos)
pickle.dump(R, f)
break
else:
pos=f.tell()
except EOFError:
f.close()
if flag:
print('Record updated successfully')
else:
print('Record not found')
except FileNotFoundError:
print('No such file')
def DELETE_REC():
try:
f1=open('Stud_DICT.dat', 'rb')
f2=open('temp.dat', 'wb')
A=int(input('Enter admission number to delete record: '))
try:
flag=False
while True:
R=pickle.load(f1)
if R['adm']==A:
flag=True
continue
else:
pickle.dump(R, f2)
except EOFError:
f1.close()
f2.close()
if flag:
os.remove('Stud_DICT.dat')
os.rename('temp.dat', 'Stud_DICT.dat')
print('Record deleted successfully')
else:
os.remove('temp.dat')
print('Record not found')
except FileNotFoundError:
print('No such file')

You might also like