0% found this document useful (0 votes)
35 views10 pages

Grade 12 CS Board Practicals BINARY

The document describes a menu-driven program to perform operations on a pickled file called 'Student.info' including adding records, displaying all records, displaying student names of a particular age, and exiting. It includes the record structure and defines functions to add records, display all records, and display names by age. The program handles file exceptions.

Uploaded by

Deeptanshu Kumar
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)
35 views10 pages

Grade 12 CS Board Practicals BINARY

The document describes a menu-driven program to perform operations on a pickled file called 'Student.info' including adding records, displaying all records, displaying student names of a particular age, and exiting. It includes the record structure and defines functions to add records, display all records, and display names by age. The program handles file exceptions.

Uploaded by

Deeptanshu Kumar
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
You are on page 1/ 10

PART I Python Programming : 08 marks

Logic 5 marks, Documentation 1 ½ marks and Code quality 1 ½ marks


PART II MYSQL : 04 marks
PART III Report File : 07 marks
PART IV Project : 08 marks
PART V Viva Voce : 03 marks
BINARY FILE
SET 4

PART I : LAB TEST- 1: (Python Programming: 8 marks)


Logic: 5 marks Documentation: 1 ½ marks Code quality: 1 ½ marks

Using user defined functions, write a Menu Based Program to perform the following
operations on a binary file called “EMP.DAT”.
1. Add a new Record. (Retain previous data)
2. Display all Records.
3. Search by EmpNo.
4. Exit
Each record has the following structure:
[empNo, ename, Salary]
import pickle as p
def add () : #function to add new record into the binary file
f = open ( "EMP.DAT" , "ab" )#binary file opened in append mode
empNo = int ( input ( "Enter Employee Number: " ) )
ename = input ( "Enter Employee Name: " )
Salary=float(input("Enter the Salary: "))
rec = [empNo, ename, Salary]
p.dump ( rec , f )
print("Record added into the file successfully")
f.close ( )

#Function to display all records


def Display () :
try :
fobj = open ( "EMP.DAT" , "rb" ) #Opening file in read mode
recordcount = 0
while True : #Loop to iterate through all records.
recordcount += 1
data = p.load ( fobj )
print ( "________________________________________________"
)
print ( ' empNo :' , data[0] )
print ( ' eName :' , data[1] )
print ( ' Salary:' , data[2] )
except EOFError :
if recordcount == 0 :
print ( "No Records found" )
fobj.close ( )
except FileNotFoundError:
print("EMP.DAT File does not exist")
#To Search and display the record by empno
def SearchEmpNo( ) :
sno= int ( input ( "Enter Employee Number whose details to be
displayed: " ) )
try :
fobj = open ( "EMP.DAT" , "rb" ) #Opening file in read mode
recordcount = 0
while True : #Loop to iterate through all records.
data = p.load ( fobj )
if data[0]==sno:
recordcount += 1
print ( "_____________________________________" )
print ( ' empNo :' , data[0] )
print ( ' eName :' , data[1] )
print ( ' Salary:' , data[2] )
except EOFError :
if recordcount == 0 :
print ( "No Records found" )
fobj.close ( )
except FileNotFoundError:
print("EMP.DAT File does not exist")
#__main__
while True :
print ( "********** ------ **********" )
print ( "\t\t\tMenu" )
print ( "********** ------ **********" )
print ( '''
1.Add Records
2.Display
3.Search by EmpNo
4.Break''' )
x = int ( input ( "Enter your choice : " ) )

if x == 1: #To add new record


add()
elif x == 2:#To view all records
Display()
elif x == 3:#To Search and display the record by empno
SearchEmpNo( )
elif x == 4: #To Exit from program
break
else :
print ( "Invalid Choice! Enter number between 1 to 4" )
SET 6
PART I : LAB TEST- 1: (Python Programming: 8 marks)
Logic: 5 marks Documentation: 1 ½ marks Code quality: 1 ½ marks
Write a Menu Based Python Program to implement modification of records in a binary
file called “Manager.DAT”. Modify the manager’s name(accept the new manager’s name
from the user) given the corresponding manager id (accept the manager ID from the
user). Take care of ‘Manager ID not found’ situation and all kinds of file related errors.
The Menu should be:
1. Add Record
2. Modify Record
3. Display Records
4. Exit
Write required user defined functions to complete the program. Each record in the file
is a dictionary object with the following structure.
Mgr={"mgr_id" : , "mgr_name": }
import pickle as p
def add () : #function to add new record into the binary file
f = open ( "Manager.dat" , "ab" ) #opened in append mode
mgid = int ( input ( "Enter Manager Id: " ) )
mgname = input ( "Enter Manager Name: " )
rec = {"mgr_id":mgid , "mgr_name":mgname}
p.dump ( rec , f )
print("Record added into the file successfully")
f.close ( )

#Modify Record WITHOUT temporary file


def Modify_WITHOUT_TempFile():
try:
mgid=int(input("Enter the manager id whose name is to be
modified:"))
fh=open( "Manager.dat" , "rb+" ) #opening original file
in read and write mode mgid=int(input("Enter the manager id
whose name is to be modified:"))
rcount=0
try:
while True: #loop to iterate through all records.
rpos = fh.tell ( )#current position
m=p.load(fh) #gets next record from file
if m['mgr_id'] == mgid : #matching record
rcount+=1
n=input("Enter new Name : ")
m['mgr_name']=n
fh.seek ( rpos , 0 )
p.dump ( m , fh )
except EOFError:
if rcount==0:
print("No Matching Records Found")
else:
print (rcount, "Record(s) Updated
Successfully!!!" )
except FileNotFoundError:
print("Manager.dat File does not exist")

#Modify Record WITH temporary file


def Modify_WITH_TempFile(): #modify name of given manager id
using temp file
try:
fh=open( "Manager.dat" , "rb" ) #opening original file in
read mode
mgid=int(input("Enter the manager id whose name is to be
modified:"))
rcount=0
fw=open("tempManager.dat","wb") #opening temporary file
in write mode
try:
while True: #loop to iterate through all records.
m=p.load(fh) #gets next record from file
if m['mgr_id'] == mgid : #matching record
rcount+=1
n=input("Enter new Name : ")
m['mgr_name']=n
p.dump ( m , fw )
except EOFError:
if rcount==0:
print("No Matching Records Found")
else:
print (rcount, "Record(s) Updated
Successfully!!!" )
fh.close ( )
fw.close ( )
import os
os.remove ( "Manager.dat" ) # deleting the original
file
os.rename ( "tempManager.dat" , "Manager.dat" ) #
renaming the temp file with original file name
except FileNotFoundError:
print("Manager.dat File does not exist")

#Function to display all records


def Display () :
try :
fobj = open ( "Manager.dat" , "rb" ) #Opening file in
read mode
recordcount = 0
while True : #Loop to iterate through all records.
data = p.load ( fobj )
recordcount += 1
print ( "_______________________________________" )
print ( ' Manager ID :' , data['mgr_id'] )
print ( ' Manager Name :' , data['mgr_name'] )
except EOFError :
if recordcount == 0 :
print ( "No Records found" )
fobj.close ( )
except FileNotFoundError:
print("Manager.dat File does not exist")

#__main__
while True :
print ( "********** ------ **********" )
print ( "\t\t\tMenu" )
print ( "********** ------ **********" )
print ( '''
1.Add Records
2.Modify
3.Display
4.Break''' )
x = int ( input ( "Enter your choice : " ) )

if x == 1: #To add new record


add()
elif x == 2:#To modify mgr name
Modify_WITHOUT_TempFile ( ) #or call
Modify_WITH_TempFile ( )
elif x == 3:#To view all records
Display ( )
elif x == 4: #To Exit from program
break
else :
print ( "Invalid Choice! Enter number between 1 to 4" )
SET 11

PART I : LAB TEST- 1: (Python Programming: 8 marks)


Logic: 5 marks Documentation: 1 ½ marks Code quality: 1 ½ marks

Using user defined functions, write a Menu Based Program to perform the following
operations on a pickled file called “Student.info”.
1. Add a new Record. (Retain previous data)
2. Display all Records.
3. Display Student Name of particular age value entered by the user.
4. Exit

Each record has the following structure:


[Studentid, stuname, age]
The program should execute as long as the user wants. Also Handle all file-based
exceptions/errors.

import pickle as p
def add () : #function to add new record into the binary file
f = open ( "Student.info" , "ab" ) #opened in append mode
Studentid = int ( input ( "Enter the student ID: " ) )
stuname = input ( "Enter the student Name: " )
age = int(input("Enter the age of the student: "))
rec = [Studentid, stuname, age]
p.dump ( rec , f )
print("Record added into the file successfully")
f.close ( )

#Function to display all records


def Display () :
try :
fobj = open ( "Student.info" , "rb" ) # read mode
recordcount = 0
while True : #Loop to iterate through all records.
recordcount += 1
data = p.load ( fobj )
print ( "___________________________________" )
print ( ' Student ID :' , data[0] )
print ( ' Student Name :' , data[1] )
print ( ' Age :' , data[2] )
except EOFError :
if recordcount == 0 :
print ( "No Records found" )
fobj.close ( )
except FileNotFoundError:
print("Student.info File does not exist")
#Display Student Name of particular age value entered by the
user.
def SearchAge( ) :
Sage= int ( input ( "Enter student age to be displayed: " ) )
try :
fobj = open ( "Student.info" , "rb" ) # read mode
recordcount = 0
while True : #Loop to iterate through all records.
data = p.load ( fobj )
if data[2]==Sage:
recordcount += 1
data = p.load ( fobj )
print ( "----------------------------" )
print ( ' Student ID :' , data[0] )
print ( ' Student Name :' , data[1] )
print ( ' Age :' , data[2] )
except EOFError :
if recordcount == 0 :
print ( "No Records found" )
fobj.close ( )
except FileNotFoundError:
print("Student.info File does not exist")
#__main__
while True :
print ( "********** ------ **********" )
print ( "\t\t\tMenu" )
print ( "********** ------ **********" )
print ( '''
1.Add Records
2.Display
3.Search by Age
4.Break''' )
x = int ( input ( "Enter your choice : " ) )

if x == 1: #To add new record


add()
elif x == 2:#To view all records
Display()
elif x == 3:#To Search and display the record by Age
SearchAge( )
elif x == 4: #To Exit from program
break
else :
print ( "Invalid Choice! Enter number between 1 to 4" )
SET 16

PART I : LAB TEST- 1: (Python Programming: 8 marks)


Logic: 5 marks Documentation: 1 ½ marks Code quality: 1 ½ marks

Using user defined functions, write a Menu Based Program to perform the following
operations on a binary file called “EMP.DAT”.
1. Add a new Record. (Retain previous data)
2. Display all Records.
3. Modify Salary of a given employee number.
4. Exit
Each record has the following structure:
[empNo, ename, Salary]
The program should execute as long as the user wants. Also Handle all file-based
exceptions/errors.
import pickle as p
def add () : #function to add new record into the binary file
f = open ( "EMP.DAT" , "ab" ) #binary file opened in append mode
empNo = int ( input ( "Enter Employee Number: " ) )
ename = input ( "Enter Employee Name: " )
Salary= float(input("Enter the salary"))
rec = [empNo, ename, Salary]
p.dump ( rec , f )
print("Record added into the file successfully")
f.close ( )

#Modify Salary of a given employee number WITHOUT temporary file


def Modify_WITHOUT_TempFile():
try:
fh=open( "EMP.DAT" , "rb+" ) #open file to read and write mode
eno=int(input("Enter the employee number to modify salary"))
rcount=0
try:
while True: #loop to iterate through all records
rpos = fh.tell ( )#get current position of pointer
e=p.load(fh) #gets next record from file
if e[0] == eno : #Checking for matching record
rcount+=1
n=input("Enter new Salary : ")
e[2]=n
fh.seek ( rpos , 0 )
p.dump ( e , fh )
except EOFError:
if rcount==0:
print("No Matching Records Found")
else:
print (rcount, "Record(s) Updated Successfully!!!" )
except FileNotFoundError:
print("EMP.DAT File does not exist")

#Modify Salary of a given employee number WITH temporary file


def Modify_WITH_TempFile():
try:
fh=open( "EMP.DAT" , "rb" ) #opening original file in read mode
eno=int(input("Enter the employee number whose salary is to be
modified:"))
rcount=0
fw=open("tempemp.dat","wb") #opening temporary file in write
mode
try:
while True: #loop to iterate through all records.
e=p.load(fh) #gets next record from file
if e[0] == eno : #Checking for matching record
rcount+=1
n=input("Enter new Salary : ")
e[2]=n
p.dump ( e , fw )
except EOFError:
if rcount==0:
print("No Matching Records Found")
else:
print (rcount, "Record(s) Updated Successfully!!!" )
fh.close ( )
fw.close ( )
import os
os.remove ( "EMP.DAT" ) # deleting the original file
os.rename ( "tempemp.dat" , "EMP.DAT" ) # renaming the
temp file with original file name
except FileNotFoundError:
print("EMP.DAT File does not exist")

#Function to display all records


def Display () :
try :
fobj = open ( "EMP.DAT" , "rb" ) #Opening file in read mode
recordcount = 0
while True : #Loop to iterate through all records.
data = p.load ( fobj )
recordcount += 1
print ( "_____________________________________________" )
print ( ' EmpNo :' , data[0] )
print ( ' eName :' , data[1] )
print ( ' Salary :' ,data[2] )
except EOFError :
if recordcount == 0 :
print ( "No Records found" )
fobj.close ( )
except FileNotFoundError:
print("EMP.DAT File does not exist")

#__main__
while True :
print ( "********** ------ **********" )
print ( "\t\t\tMenu" )
print ( "********** ------ **********" )
print ( '''
1.Add Records
2.Display
3.Modify
4.Break''' )
x = int ( input ( "Enter your choice : " ) )

if x == 1: #To add new record


add()
elif x == 2:#To view all records
Display ( )
elif x == 3:
#Modify_WITH_TempFile ( ) OR CALL
Modify_WITHOUT_TempFile ( )
elif x == 4: #To Exit from program
break
else :
print ( "Invalid Choice! Enter number between 1 to 4" )

You might also like