Binary File Project
Binary File Project
import pickle
import sys # for exit
##############################################
########################
##1 Add New Student Records
def AddStudent() :
bfile=open("empfile.dat","ab")
print ("Enter Records of Employees")
#taking data from user and dumping in the file as list object
eno=int(input("Employee number : "))
ename=input("Employee Name : ")
gender=input("Enter your gender m/f")
ebasic=int(input("Basic Salary : "))
allow=int(input("Allowances : "))
totsal=ebasic+allow
empdata=[eno,ename,gender,ebasic,allow,totsal]
pickle.dump(empdata,bfile)
bfile.close()
##############################################
##################
readrec=1
try:
with open("empfile.dat","rb") as bfile:
while True:
empdata=pickle.load(bfile)
print("Record Number : ",readrec, "# ", end="")
print(empdata)
readrec=readrec+1
except EOFError:
pass
bfile.close()
##############################################
######################
if not found:
print("Employee Record Not Found!")
except FileNotFoundError:
print("File not found! Please add records first.")
except ValueError:
print("Invalid input! Please enter the correct data type.")
###############
1
def SearchRecord():
try:
with open("empfile.dat", "rb") as bfile:
eno_to_search = int(input("Enter Employee Number to Search: "))
found = False
while True:
try:
empdata = pickle.load(bfile)
if empdata[0] == eno_to_search: # Check if employee number
matches
found = True
print("Employee Found:")
print(f"Employee Number: {empdata[0]}")
print(f"Employee Name: {empdata[1]}")
print(f"Gender: {empdata[2]}")
print(f"Basic Salary: {empdata[3]}")
print(f"Allowances: {empdata[4]}")
print(f"Total Salary: {empdata[5]}")
break
except EOFError:
break
if not found:
print("Employee Not Found.")
except FileNotFoundError:
print("Error: Employee file not found.")
import pickle
def DeleteRecord():
"""Deletes a record from the binary file."""
try:
with open("empfile.dat", "rb") as infile:
records = []
eno_to_delete = int(input("Enter Employee Number to Delete: "))
found = False
while True:
try:
record = pickle.load(infile)
if record[0] != eno_to_delete:
records.append(record) # Keep records that don't match
else:
found = True
except EOFError:
break
if found:
with open("empfile.dat", "wb") as outfile:
for record in records:
pickle.dump(record, outfile) # Write remaining records
print(f"Employee with number {eno_to_delete} deleted.")
else:
print("Employee Not Found.")
except FileNotFoundError:
print("Error: Employee file not found.")
# ... (rest of your main menu and other functions) ...
choice=None
while True :
print("1 Add new Reocrds in Binary File")
print("2 Display Records of Empployee")
print("3 Modify/Update Records of Empployee")
print("4 Search Records of Empployee")
print("5 Delete Records of Empployee")
print("0 Exit")
if choice==1 :
AddStudent()
if choice==2 :
DisplayRecord()
if choice==3 :
ModifyRecord()
if choice==4 :
SearchRecord()
if choice==5 :
DeleteRecord()
if choice==0 :
print("Thank You !!!")
sys.exit(0)