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

Student Database System

Project To main student data like roll, name and per

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_roll

search_name()

delete_roll()

delete_name()

update_roll()

login()

main()

"""
Binary File 
To maintain Student details like

Roll
Name
Per
Operations:
Add record
Display All Records
Search a record by Roll no
Search a record by name
delete a record
Modify a record

"""

import os
import pickle
from getpass import getpass

#add_record() : to add a new record
def add_record():
	#take input for student details
	try:
		if os.path.isfile("stud"):
			print("file is present")
			f=open("stud","ab")
		else:
			print("file is not present, has to be created")
			f=open("stud","wb")
		#roll=input("Enter roll no ")
		#name=input("Enter name ")
		#per=input("Enter per ")
		#input of data with validation

		#roll no input
		while True:
			z=0
			roll=input("Enter roll no (4 digit) :")
			if not roll.isdigit():
				print("Rollno Has to be Numeric")
				input("Press Enter to input again...")
				z=1
				continue
			if not (int(roll)>=1000 and int(roll)<=9999):
				print("Rollno 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
	
		# per input
		while True:
			z=0
			per=input("Enter per  :")
			if not per.isdigit():
				print("Per Has to be Numeric")
				input("Press Enter to input again...")
				z=1
				continue
			if not (int(per)>=0 and int(per)<=100):
				print("Rollno Has to be >=0 and <=100")
				input("Press Enter to input again...")
				z=1
				continue
			if z!=1:
				break

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

#to display heading
def heading():
	print(" "*30," Student Details ")
	print(" "*30,"================= ")
	print(" "*10,"="*60)
	print(" "*10,"Roll No"," "*10," Name "," "*10,"Per")
	print(" "*10,"="*60)

#to display all the records
#method :1
def display_all():
	try:
		f=open("stud","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("stud","rb")
		os.system("cls")
		print(" "*30," Student Details ")
		print(" "*30,"================= ")
		print(" "*10,"="*60)
		print(" "*10,"Roll No"," "*10," Name "," "*10,"Per")
		print(" "*10,"="*60)
		while True:
			rec=pickle.load(f)
			print(" "*10,rec[0]," "*15,rec[1]," "*15,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_roll():
	z=0
	try:
		tr=input("Enter roll no to search ")
		f=open("stud","rb")
		os.system("cls")
		print(" "*30," Student Details ")
		print(" "*30,"================= ")
		print(" "*10,"="*60)
		print(" "*10,"Roll No"," "*10," Name "," "*10,"Per")
		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(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 ")
		f=open("stud","rb")
		os.system("cls")
		print(" "*30," Student Details ")
		print(" "*30,"================= ")
		print(" "*10,"="*60)
		print(" "*10,"Roll No"," "*10," Name "," "*10,"Per")
		print(" "*10,"="*60)
		while True:
			rec=pickle.load(f)
			if tn==rec[1]:
				z=1
				print(" "*10,rec[0]," "*15,rec[1]," "*15,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_roll():
	os.system("cls")
	z=0
	try:
		tr=input("Enter roll no to delete ")
		f=open("stud","rb")
		tf=open("temp","wb")
		
		
		while True:
			rec=pickle.load(f)
			if tr==rec[0]:
				print(" "*30," Student Details ")
				print(" "*30,"================= ")
				print(" "*10,"="*60)
				print(" "*10,"Roll No"," "*10," Name "," "*10,"Per")
				print(" "*10,"="*60)
				z=1
				print(" "*10,rec[0]," "*15,rec[1]," "*15,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("stud")
			os.rename("temp","stud")
			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("stud","rb")
		tf=open("temp","wb")
		
		
		while True:
			rec=pickle.load(f)
			if tn==rec[1]:
				print(" "*30," Student Details ")
				print(" "*30,"================= ")
				print(" "*10,"="*60)
				print(" "*10,"Roll No"," "*10," Name "," "*10,"Per")
				print(" "*10,"="*60)
				z=1
				print(" "*10,rec[0]," "*15,rec[1]," "*15,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("stud")
			os.rename("temp","stud")
			print("Record Deleted")


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

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

def update_roll():
	os.system("cls")
	z=0
	z1=0
	try:
		tr=input("Enter roll no to Update ")
		f=open("stud","rb")
		tf=open("temp","wb")
		
		
		while True:
			z=0
			rec=pickle.load(f)
			if tr==rec[0]:
				print(" "*30," Student Details ")
				print(" "*30,"================= ")
				print(" "*10,"="*60)
				print(" "*10,"Roll no"," "*10," Name "," "*10,"Per")
				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")
				roll=input("Enter Roll No ")
				name=input("Enter name ")
				per=input("Enter Per ")
				st=[roll,name,per]
				pickle.dump(st,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("stud")
			os.rename("temp","stud")
			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 Roll No
	5. Search By Name
	6. Delete by Roll No
	7. Delete By Name
	8. Update using Roll 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_roll()
		elif ch=='5':
			search_name()
		elif ch=='6':
			delete_roll()
		elif ch=='7':
                        
			delete_name()
		elif ch=='8':
			update_roll()
		elif ch=='0':
			print("End")
			break
		else:
			print("Invalid choice")

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