0% found this document useful (0 votes)
8 views2 pages

Simplified Binary File Program

This document contains a Python script for managing student records using the pickle module for serialization. It provides functions to add, display, modify, and delete records based on roll numbers. The script includes a menu-driven interface for user interaction.

Uploaded by

binamazu1980
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)
8 views2 pages

Simplified Binary File Program

This document contains a Python script for managing student records using the pickle module for serialization. It provides functions to add, display, modify, and delete records based on roll numbers. The script includes a menu-driven interface for user interaction.

Uploaded by

binamazu1980
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

import pickle

def add_record():
roll = int(input("Enter Roll Number: "))
name = input("Enter Name: ")
with open("[Link]", "ab") as file:
[Link]([roll, name], file)

def display_records():
try:
with open("[Link]", "rb") as file:
while True:
try:
record = [Link](file)
print("Roll:", record[0], "Name:", record[1])
except EOFError:
break
except FileNotFoundError:
print("No records found.")

def modify_record(roll_no):
records = []
found = False
try:
with open("[Link]", "rb") as file:
while True:
try:
record = [Link](file)
if record[0] == roll_no:
record[1] = input("Enter new name: ")
found = True
[Link](record)
except EOFError:
break
except FileNotFoundError:
print("No records found.")
return

with open("[Link]", "wb") as file:


for record in records:
[Link](record, file)

if found:
print("Record modified.")
else:
print("Roll number not found.")

def delete_record(roll_no):
records = []
found = False
try:
with open("[Link]", "rb") as file:
while True:
try:
record = [Link](file)
if record[0] != roll_no:
[Link](record)
else:
found = True
except EOFError:
break
except FileNotFoundError:
print("No records found.")
return
with open("[Link]", "wb") as file:
for record in records:
[Link](record, file)

if found:
print("Record deleted.")
else:
print("Roll number not found.")

while True:
print("\nMenu:")
print("1. Add Record")
print("2. Display Records")
print("3. Modify Record")
print("4. Delete Record")
print("5. Exit")
choice = int(input("Enter your choice: "))

if choice == 1:
add_record()
elif choice == 2:
display_records()
elif choice == 3:
rn = int(input("Enter roll number to modify: "))
modify_record(rn)
elif choice == 4:
rn = int(input("Enter roll number to delete: "))
delete_record(rn)
elif choice == 5:
break
else:
print("Invalid choice.")

You might also like