0% found this document useful (0 votes)
1 views4 pages

Binary File Program

The document outlines a Python program for an Employee Record Management System that utilizes a binary file named 'employee.dat' to store employee records. The program provides a menu-driven interface to add, display, search, update, and delete employee records based on their employee number. It employs the 'pickle' module for serialization and deserialization of the records, ensuring data persistence.

Uploaded by

Gurleen
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)
1 views4 pages

Binary File Program

The document outlines a Python program for an Employee Record Management System that utilizes a binary file named 'employee.dat' to store employee records. The program provides a menu-driven interface to add, display, search, update, and delete employee records based on their employee number. It employs the 'pickle' module for serialization and deserialization of the records, ensuring data persistence.

Uploaded by

Gurleen
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

'''Binary file program for Employee Record Management System

Create a binary file [Link] containing the following fields: EmpNo, Name, Salary Menu Driven
Program to:
Add a record
Display all records
Search a record by EmpNo
Update salary of an employee
Delete a record '''

# Employee Record Management System

import pickle
import os

FILENAME = "[Link]"

# -----------------------------
# Function to Add Record
# -----------------------------
def add_record():
f = open(FILENAME, "ab") # append in binary mode
empno = int(input("Enter Employee Number: "))
name = input("Enter Name: ")
salary = float(input("Enter Salary: "))

record = [empno, name, salary]


[Link](record, f)

[Link]()
print("Record added successfully.\n")

# -----------------------------
# Function to Display Records
# -----------------------------
def display_record():
try:
f = open(FILENAME, "rb")
print("\nEmployee Records:")
print("-------------------")
while True:
record = [Link](f)
print("EmpNo:", record[0],
"Name:", record[1],
"Salary:", record[2])
except EOFError:
[Link]()
except FileNotFoundError:
print("File does not exist.\n")

# -----------------------------
# Function to Search Record
# -----------------------------
def search_record():
try:
f = open(FILENAME, "rb")
empno = int(input("Enter EmpNo to search: "))
found = False

while True:
record = [Link](f)
if record[0] == empno:
print("Record Found:")
print("EmpNo:", record[0])
print("Name:", record[1])
print("Salary:", record[2])
found = True
break
except EOFError:
if not found:
print("Record not found.")
[Link]()
except FileNotFoundError:
print("File does not exist.\n")

# -----------------------------
# Function to Update Salary
# -----------------------------
def update_record():
try:
f = open(FILENAME, "rb")
temp = open("[Link]", "wb")
empno = int(input("Enter EmpNo to update salary: "))
found = False

while True:
record = [Link](f)
if record[0] == empno:
print("Old Salary:", record[2])
record[2] = float(input("Enter New Salary: "))
found = True
[Link](record, temp)
except EOFError:
[Link]()
[Link]()

if found:
[Link](FILENAME)
[Link]("[Link]", FILENAME)
print("Salary updated successfully.\n")
else:
[Link]("[Link]")
print("Record not found.\n")
except FileNotFoundError:
print("File does not exist.\n")

# -----------------------------
# Function to Delete Record
# -----------------------------
def delete_record():
try:
f = open(FILENAME, "rb")
temp = open("[Link]", "wb")
empno = int(input("Enter EmpNo to delete: "))
found = False

while True:
record = [Link](f)
if record[0] != empno:
[Link](record, temp)
else:
found = True
except EOFError:
[Link]()
[Link]()

if found:
[Link](FILENAME)
[Link]("[Link]", FILENAME)
print("Record deleted successfully.\n")
else:
[Link]("[Link]")
print("Record not found.\n")
except FileNotFoundError:
print("File does not exist.\n")

# -----------------------------
# Main Menu
# -----------------------------
while True:
print("\n===== Employee Record Menu =====")
print("1. Add Record")
print("2. Display All Records")
print("3. Search Record")
print("4. Update Salary")
print("5. Delete Record")
print("6. Exit")

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

if choice == 1:
add_record()
elif choice == 2:
display_record()
elif choice == 3:
search_record()
elif choice == 4:
update_record()
elif choice == 5:
delete_record()
elif choice == 6:
print("Exiting Program...")
break
else:
print("Invalid Choice. Try again.\n")

You might also like