CBSE Class XII: Python Data File Handling| Binary File Projects

Employee Database System

Project To main employee data like empno, name and salary

Operations:
========

Add record
Display All Records
Search a record by Roll no
Search a record by name
delete a record
Modify a record

Functions:
add_record()

heading()

display_all()

display_all2()

search_empno()

search_name()

delete_empno()

delete_name()

update_empno()

login()

main()

"""
Binary File 
To maintain Bank details like

empno
name
sal

Operations:
Add record
Display All Records
Search a record by empno
Search a record by name
delete a record
Modify a record
Update record
"""

import os
import pickle
from getpass import getpass

#add_record() : to add a new record
def add_record():
	#take input for employee details
	try:
		if os.path.isfile("emp"):
			print("file is present")
			f=open("emp","ab")
		else:
			print("file is not present, has to be created")
			f=open("emp","wb")
		#empno=input("Enter Employee No ")
		#name=input("Enter name ")
		#sal=input("Enter Salary ")
		#input of data with validation

		#empno no input
		while True:
			z=0
			empno=input("Enter empno (4 digit) :")
			if not empno.isdigit():
				print("empno Has to be Numeric")
				input("Press Enter to input again...")
				z=1
				continue
			if not (int(empno)>=1000 and int(empno)<=9999):
				print("empno Has to be in 4 digitsd")
				input("Press Enter to input again...")
				z=1
				continue
			if z!=1:
				break

		# Name input
		while True:
			z=0
			name=input("Enter Name :")
			if not name.isalpha():
				print("Name Has to be Alphabetic")
				input("Press Enter to input again ... ")
				z=1
				continue
			if z!=1:
				break
	
		# salance input
		while True:
			z=0
			sal=input("Enter salance  :")
			if not sal.isdigit():
				print("salary Has to be Numeric")
				input("Press Enter to input again...")
				z=1
				continue
			if not (int(sal)>5000):
				print("salance Has >=5000 ")
				input("Press Enter to input again...")
				z=1
				continue
			if z!=1:
				break

		#to save the data in file
		em=[empno,name,sal]
		pickle.dump(em,f)
		f.close()
	
	except IOError:
		printf("Unable to open the file")

#to display heading
def heading():
	print(" "*30," Employee Details ")
	print(" "*30,"================= ")
	print(" "*10,"="*60)
	print(" "*10,"empno"," "*10," Name "," "*10,"Salary")
	print(" "*10,"="*60)

#to display all the records
#method :1
def display_all():
	try:
		f=open("emp","rb")
		while True:
			rec=pickle.load(f)
			print(rec)
			#input("Press Enter to cont ...")


	except EOFError:
		f.close()
		input("Press Enter to cont ...")

	except IOError:
		printf("Unable to open the file")
	
#to display all the records
#method :2	
def display_all2():
	try:
		f=open("emp","rb")
		os.system("cls")
		print(" "*30," Employee Details ")
		print(" "*30,"================= ")
		print(" "*10,"="*60)
		print(" "*10,"empno"," "*10," Name "," "*10,"Salary")
		print(" "*10,"="*60)
		while True:
			rec=pickle.load(f)
			#print(" "*10,rec[0]," "*15,rec[1]," "*15,rec[2])
			print(" "*10,"%-10s"%rec[0],"%-20s"%rec[1],"%-10s"%rec[2])
			#print(rec[0],rec[1],rec[2])
			#input("Press Enter to cont ...")


	except EOFError:
		print(" "*10,"="*60)
		f.close()
		input("Press Enter to cont ...")

	except IOError:
		printf("Unable to open the file")


def search_empno():
	z=0
	try:
		tr=input("Enter empno no to search ")
		f=open("emp","rb")
		os.system("cls")
		print(" "*30," Employee Details ")
		print(" "*30,"================= ")
		print(" "*10,"="*60)
		print(" "*10,"empno"," "*10," Name "," "*10,"Salary")
		print(" "*10,"="*60)
		while True:
			rec=pickle.load(f)
			if tr==rec[0]:
				z=1
				#print(" "*10,rec[0]," "*15,rec[1]," "*15,rec[2])
				print(" "*10,"%-10s"%rec[0],"%-20s"%rec[1],"%-10s"%rec[2])
			#print(rec[0],rec[1],rec[2])
			#input("Press Enter to cont ...")


	except EOFError:
		print(" "*10,"="*60)
		f.close()
		if z==0:
			print("Record Not Presnt")
		input("Press Enter to cont ...")

	except IOError:
		printf("Unable to open the file")

def search_name():
	z=0
	try:
		tn=input("Enter Name to search ")
		tn.lower()
		f=open("emp","rb")
		os.system("cls")
		print(" "*30," Employee Details ")
		print(" "*30,"================= ")
		print(" "*10,"="*60)
		print(" "*10,"empno"," "*10," Name "," "*10,"Salary")
		print(" "*10,"="*60)
		while True:
			rec=pickle.load(f)
			if tn==rec[1].lower():
				z=1
				#print(" "*10,rec[0]," "*15,rec[1]," "*15,rec[2])
				print(" "*10,"%-10s"%rec[0],"%-20s"%rec[1],"%-10s"%rec[2])
			#print(rec[0],rec[1],rec[2])
			#input("Press Enter to cont ...")


	except EOFError:
		print(" "*10,"="*60)
		f.close()
		if z==0:
			print("Record Not Presnt")
		input("Press Enter to cont ...")

	except IOError:
		printf("Unable to open the file")

def delete_empno():
	os.system("cls")
	z=0
	try:
		tr=input("Enter empno no to delete ")
		f=open("emp","rb")
		tf=open("temp","wb")
		
		
		while True:
			rec=pickle.load(f)
			if tr==rec[0]:
				print(" "*30," Employee Details ")
				print(" "*30,"================= ")
				print(" "*10,"="*60)
				print(" "*10,"empno"," "*10," Name "," "*10,"Salary")
				print(" "*10,"="*60)
				z=1
				#print(" "*10,rec[0]," "*15,rec[1]," "*15,rec[2])
				print(" "*10,"%-10s"%rec[0],"%-20s"%rec[1],"%-10s"%rec[2])
			else:
				pickle.dump(rec,tf)				
			#print(rec[0],rec[1],rec[2])
			#input("Press Enter to cont ...")


	except EOFError:
		print(" "*10,"="*60)
		f.close()
		tf.close()
		if z==0:
			print("Record Not Presnt")
		else:
			os.remove("emp")
			os.rename("temp","emp")
			print("Record Deleted")


		input("Press Enter to cont ...")

	except IOError:
		printf("Unable to open the file")

def delete_name():
	os.system("cls")
	z=0
	try:
		tn=input("Enter Name to delete ")
		f=open("emp","rb")
		tf=open("temp","wb")
		
		
		while True:
			rec=pickle.load(f)
			if tn==rec[1]:
				print(" "*30," Employee Details ")
				print(" "*30,"================= ")
				print(" "*10,"="*60)
				print(" "*10,"empno"," "*10," Name "," "*10,"Salary")
				print(" "*10,"="*60)
				z=1
				#print(" "*10,rec[0]," "*15,rec[1]," "*15,rec[2])
				print(" "*10,"%-10s"%rec[0],"%-20s"%rec[1],"%-10s"%rec[2])
			else:
				pickle.dump(rec,tf)				
			#print(rec[0],rec[1],rec[2])
			#input("Press Enter to cont ...")


	except EOFError:
		print(" "*10,"="*60)
		f.close()
		tf.close()
		if z==0:
			print("Record Not Presnt")
		else:
			os.remove("emp")
			os.rename("temp","emp")
			print("Record Deleted")


		input("Press Enter to cont ...")

	except IOError:
		printf("Unable to open the file")

def update_empno():
	os.system("cls")
	z=0
	z1=0
	try:
		tr=input("Enter empno no to Update ")
		f=open("emp","rb")
		tf=open("temp","wb")
		
		
		while True:
			z=0
			rec=pickle.load(f)
			if tr==rec[0]:
				print(" "*30," Employee Details ")
				print(" "*30,"================= ")
				print(" "*10,"="*60)
				print(" "*10,"empno"," "*10," Name "," "*10,"Salary")
				print(" "*10,"="*60)
				z=1
				z1=1
				#print(" "*10,rec[0]," "*15,rec[1]," "*15,rec[2])
				print(" "*10,"%-10s"%rec[0],"%-20s"%rec[1],"%-10s"%rec[2])

				print("\n\n Enter new Details\n")
				empno=input("Enter Employee No ")
				name=input("Enter name ")
				sal=input("Enter Salary ")
				em=[empno,name,sal]
				pickle.dump(em,tf)				

			if z==0:
				pickle.dump(rec,tf)				
			#print(rec[0],rec[1],rec[2])
			#input("Press Enter to cont ...")


	except EOFError:
		print(" "*10,"="*60)
		f.close()
		tf.close()
		if z1==0:
			print("Record Not Presnt")
		else:
			os.remove("emp")
			os.rename("temp","emp")
			print("Record Updated")


		input("Press Enter to cont ...")

	except IOError:
		printf("Unable to open the file")



def login():
	os.system("cls")
	print()
	print()
	print()
	print("   Login Window")
	print("******************")
	print("="*40)
	u=input("Enter UserName  : ")
	p=getpass("Enter Password  : ")
	if (u=="ct" and p=="ct"):
		main()
	else:
		print("Invalid user")
		exit()
		

def main():
	while True:
		os.system("cls")
		m="""
	Main Menu
	=========
	1. Add Record
	2. Display All 1
	3. Display All 2
	4. Search By Employee No
	5. Search By Employee Name
	6. Delete by Employee No
	7. Delete By Employee Name
	8. Update using Employee No 
	0. Exit
	"""
		print(m)
		ch=input("Enter your choice : ")
		if ch==" ":
			continue

		if ch=='1':
			add_record()
		elif ch=='2':
			display_all()
		elif ch=='3':
			display_all2()
		elif ch=='4':
			search_empno()
		elif ch=='5':
			search_name()
		elif ch=='6':
			delete_empno()
		elif ch=='7':
			delete_name()
		elif ch=='8':
			update_empno()
		elif ch=='0':
			print("End")
			break
		else:
			print("Invalid choice")

		

if __name__=='__main__':
	#main()
	login()