Binary File Rec As List and Nested List
Binary File Rec As List and Nested List
Binary File Rec As List and Nested List
'''
def writedata():
f=open("StudentDetails.dat","wb")
record=[]
while True:
rno=int(input("Enter the Roll No : "))
name=input("Enter the Name : ")
marks=int(input("Emter the Marks : "))
data=[rno,name,marks]
record.append(data)
ch=input("More records (Y/N) ? ")
if ch in 'Nn':
break
pickle.dump(record,f)
print("Record writen in the File ...")
f.close()
def readdata():
print("Contents of the Binary file ...")
f=open("StudentDetails.dat","rb")
try:
while True:
s=pickle.load(f)
print(s)
print()
for i in s:
print(i)
except Exception:
f.close()
print("File Closed ...")
def appenddata():
f=open("StudentDetails.dat","rb+")
print("appending data in the File...")
Rec=pickle.load(f)# this will read the contents of the file and put it in rec list
while True:
rno=int(input("Enter the Roll No : "))
name=input("Enter the Name : ")
marks=int(input("Emter the Marks : "))
data=[rno,name,marks]
Rec.append(data)
ch=input("More records (Y/N) ? ")
if ch in 'Nn':
break
f.seek(0)# take the ponter to the beging of the file
pickle.dump(Rec,f)
print("Record Appended ...")
f.close()
def searchdata():
f=open("StudentDetails.dat","rb") #statement 1
r=int(input("Enter the roll no to be searched "))
found=0
try:
while True:
s=pickle.load(f) # statement 2
print(s)
print("\n")
for i in s: # statement 3
if i[0]==r: # statement 4
print(i)
found=1
break
except EOFError:
print("Reading of the file is complete ")
f.close() # ststement 5
if found==0:
print(" Record NOT Found ")
def updatedata():
f=open("StudentDetails.dat","rb+")
r=int(input("Enter the roll no to be searched "))
f.seek(0)# take the file pointer to the zero th position
try:
while True:
rpos=f.tell()# it will give 0 as the position ie begining
s=pickle.load(f)
print(s)
for i in s:
if i[0]==r:
print(i)
print("This is the record you wish to modify/update ")
i[1]=input("Enter the new name ")
i[2]=int(input("Enter the new marks "))
f.seek(rpos) # go to 0 th byte or begining of file
pickle.dump(s,f)
print("Your record has been updated ")
break
except Exception:
f.close()
def deletedata():
f=open("StudentDetails.dat","rb") # to jusr read from file
s=pickle.load(f)
f.close()
r=int(input("Enter the roll no to be deleted "))
f=open("StudentDetails.dat","wb") # to write freshly in the file
print(s) # printing the content of the file
print("\n\n")
reclst=[]
for i in s:
if i[0]==r:
continue
reclst.append(i)
pickle.dump(reclst,f)
print("\n your record has been deleted ")
f.close()
def MainMenu():
print("----------------------------------------")
print("1. Write data in a Binary File...")
print("2. Read data from a Binary File...")
print("3. Append data in a Binary File...")
print("4. Search data in a Binary File...")
print("5. Update/Modify data in a Binary File...")
print("6. Delete data from a Binary File...")
print("7. Exit...")
print("----------------------------------------")
while True: # it is a loop which will run infinite times with no pre condition
MainMenu() # call to the MainMenu function
ch=int(input("Enter your choice : "))
if ch==1:
writedata() # call to write data into the file
elif ch==2:
readdata()
elif ch==3:
appenddata()
elif ch==4:
searchdata()
elif ch==5:
updatedata()
elif ch==6:
deletedata()
elif ch==7:
break
'''
# write a prog to write, append, Read and Delete data from a binary file
# To read records and write as a single list at a time in the file
#write in the file
# a record as a seperate list with rollno,name and marks in three stubjects
'''
import pickle
f=open("Stud.dat","ab")
#l=[]
while True:
l=[]
roll=int(input("Enter roll No "))
l.append(roll)
name=input("Enter name ")
l.append(name)
marks=[]
for i in range(0,3):
x=float(input("Enter marks "))
marks.append(x)
l.append(marks)
pickle.dump(l,f)
ch=input("More records (Y/N) ? ")
if ch in 'Nn':
break
#pickle.dump(l,f)
print("Record writen in the File ...")
f.close()
'''
'''
# Search and Append
# with no duplication of Roll no
import pickle
ans='y'
while ans=='y':
l=[]
roll=int(input("Enter roll No "))
try:
found=0
f=open("Stud.dat","rb")
while True:
s=pickle.load(f)
if s[0]==roll:
print("Roll number exists ")
found=1
break
except EOFError:
f.close()
if found==0:
l.append(roll)
f=open("Stud.dat","ab+")
name=input("Enter name ")
l.append(name)
marks=[]
for i in range(0,3):
x=float(input("Enter marks "))
marks.append(x)
l.append(marks)
pickle.dump(l,f)
ans=input("Enter y to continue ")
f.close()
'''
'''
# Read
import pickle
f=open("Stud.dat","rb")
f.seek(0)
try:
while True:
s=pickle.load(f)
print(s)
except:
f.close()
print("The End")
'''
#delete
'''
import pickle
import os
roll=int(input("Enter the roll number to be deleted "))
f=open("Stud.dat","rb")
f1=open("Temp.dat","wb")
try:
while True:
s=pickle.load(f)
if s[0]!=roll:
pickle.dump(s,f1)
except Exception:
f1.close()
f.close()
os.remove("Stud.dat")
os.rename("Temp.dat","Stud.dat")
'''