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

Binary File Project

The document outlines a Python program for managing employee records using binary files and the pickle module. It includes functions to add, display, modify, search, and delete employee records, with user input for various employee details. The program features a simple menu for user interaction and handles file operations with error checking for file existence and input validity.

Uploaded by

mobileunbox88
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)
28 views7 pages

Binary File Project

The document outlines a Python program for managing employee records using binary files and the pickle module. It includes functions to add, display, modify, search, and delete employee records, with user input for various employee details. The program features a simple menu for user interaction and handles file operations with error checking for file existence and input validity.

Uploaded by

mobileunbox88
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

# 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

print("\tTOTAL SALARY : ", totsal)

empdata=[eno,ename,gender,ebasic,allow,totsal]
pickle.dump(empdata,bfile)

bfile.close()
##############################################
##################

### 2 Display Reords of Employee


def DisplayRecord():
# Reading the employee records from the file using load() module
print("Now reading the employee records from the file")

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

##############################################
######################

### 3 Modify Records of employee


def ModifyRecord():
try:
# Open the binary file to read all records into a list
with open('empfile.dat', 'rb') as bfile:
record_list = []
while True:
try:
record_list.append(pickle.load(bfile))
except EOFError:
break

# Ask for the Employee Number to modify


eno = int(input("Enter Employee Number to Modify: "))
found = False # Flag to check if the record exists

# Iterate through the records to find the one to update


for i in range(len(record_list)):
if record_list[i][0] == eno: # Compare Employee Number
found = True
print("Current Record Found:")
print(f"Emp No: {record_list[i][0]}, Name: {record_list[i][1]},
Gender: {record_list[i][2]}, "
f"Basic: {record_list[i][3]}, Allowances: {record_list[i][4]},
Total: {record_list[i][5]}")

# Take new details as input


record_list[i][1] = input("Enter New Name: ") or record_list[i][1]
record_list[i][2] = input("Enter New Gender (m/f): ").lower() or
record_list[i][2]
record_list[i][3] = float(input("Enter New Basic Salary: ") or
record_list[i][3])
record_list[i][4] = float(input("Enter New Allowances: ") or
record_list[i][4])
record_list[i][5] = record_list[i][3] + record_list[i][4] # Update
total salary
print("Record Updated Successfully!")
break

if not found:
print("Employee Record Not Found!")

# Write updated records back to the file


with open('empfile.dat', 'wb') as bfile:
for record in record_list:
pickle.dump(record, bfile)

except FileNotFoundError:
print("File not found! Please add records first.")
except ValueError:
print("Invalid input! Please enter the correct data type.")

######## Main menu ############

###############
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")

choice=int(input("Enter your choice"))

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)

You might also like