Binary File and Text File Programs For The Practical Exam-2023
Binary File and Text File Programs For The Practical Exam-2023
Set 1
(a) Write a menu driven program to perform the following operations on a binary file “emp.dat”
has structure [EID, EName, Designation, Salary].
i. Write a user defined function CreateEmp() to input data for a record and create a file
“emp.dat”
ii. Write a function display() to display the details of all employees, whose salary is more
than 30000.
iii. Display the file content.
iv. Exit
Ans.:
import pickle
def CreateEmp():
n=int(input("Enter number of employees: "))
f1=open("emp.dat",'wb')
for i in range(n):
eid=input("Enter E. Id: ")
ename=input("Enter Name: ")
designation=input("Enter Designation: ")
salary=int(input("Enter Salary: "))
rec=[eid,ename,designation,salary]
pickle.dump(rec,f1)
f1.close()
# Part 2
import pickle
def display_sal_above_30k():
f2=open("emp.dat","rb")
found=0
try:
while True:
rec=pickle.load(f2)
if rec[3]>30000:
print("E. Id:",rec[0])
print("Name:",rec[1])
Page 1 of 15
print("Post:",rec[2])
print("Salary:",rec[3])
found=1
except:
f2.close()
if found==0:
print("No record found !")
#Part 3
def display():
print("Contents in a Binary file ")
f=open("emp.dat","rb")
try:
print("ID\t Name\t Post \t Salary")
while True:
R=pickle.load(f)
print(R[0],'\t',R[1],'\t',R[2],'\t',R[3])
#print(R)
except:
f.close()
def main():
while True:
print("-------MAIN MENU-----")
print("1. Create File ")
print("2. Display File")
print("3. Display Employees where salary is more than 30000")
print("4. Exit")
print("---------------------")
choice=int(input("Enter your choice (1..4) : "))
if choice==1:
CreateEmp()
elif choice==2:
display()
elif choice==3:
display_sal_above_30k()
elif choice==4:
exit()
else:
print("Invalid Choice ! ")
main()
Page 2 of 15
Set 2
Write a menu driven program to perform the following operations on a text file.
i. Write a user defined function to create a text file DATA1.TXT and display the file
content.
ii. Write a function to count the number of lines in a text file DATA1.TXT which starts and
ends with “T” and “e” respectively.
iii. Display the file.
iv. Exit
def create_file1():
fil=open("data1.txt","w+")
n=int(input("Enter number of lines : "))
for i in range(n):
data= input("Enter data to save in the text file: ")
fil.write(data)
fil.write('\n')
fil.close()
def count_Lines_starting_with_T_and_Ending_with_e():
f1=open('data1.txt','r')
lines=f1.readlines()
count=0
i=1
print("Displaying the lines Starting with T and ending with e : ")
for line in lines:
i+=1
if line[0]=='T' and line[-2]=='e':
print(line)
count+=1
print("Number of Lines starting with 'T' and ending with 'e' is ", count)
def main():
Page 3 of 15
while True:
print("-------MAIN MENU-----")
print("1. Create File ")
print("2. Display File")
print("3. Counting and Displaying Lines starting with 'T' and ending with 'e' ")
print("4.Exit")
print("---------------------")
choice=int(input("Enter your choice (1..5) : "))
if choice==1:
create_file1()
elif choice==2:
display_file1()
elif choice==3:
count_Lines_starting_with_T_and_Ending_with_e()
elif choice==4:
exit()
else:
print("Invalid Choice ! ")
main()
Page 4 of 15
Set 3
Write a menu driven program to perform the following operations on a binary file “emp.dat”
has structure [EID, EName, Designation, Salary].
i. Write a user defined function to add more records of employees in the existing file
“emp.dat”
ii. Write a function Show_Salesman() to display the details of all employees, whose
designation is “Salesman”.
iii. Display the file content.
iv. Exit
import pickle
def CreateEmp():
n=int(input("Enter number of employees: "))
f1=open("emp.dat",'ab')
for i in range(n):
eid=input("Enter E. Id: ")
ename=input("Enter Name: ")
designation=input("Enter Designation: ")
salary=int(input("Enter Salary: "))
rec=[eid,ename,designation,salary]
pickle.dump(rec,f1)
f1.close()
# Part 2
import pickle
def Show_Salesmen():
f2=open("emp.dat","rb")
print("Displaying salesmen details: ")
found=0
try:
while True:
rec=pickle.load(f2)
if rec[2]=="Salesman":
print("ID: ",rec[0])
print("Name: ",rec[1])
Page 5 of 15
print("Post: ",rec[2])
print("Salary: ", rec[3])
found=1
except:
f2.close()
if found==0:
print("No record found !")
def display():
print("Contents in a Binary file ")
f=open("emp.dat","rb")
try:
print("ID\t Name\t Post \t Salary")
while True:
R=pickle.load(f)
print(R[0],'\t',R[1],'\t',R[2],'\t',R[3])
#print(R)
except:
f.close()
def main():
while True:
print("-------MAIN MENU-----")
print("1. Create File ")
print("2. Display File")
print("3. Display Employees who are working as Salesman")
print("4. Exit")
print("---------------------")
choice=int(input("Enter your choice (1..4) : "))
if choice==1:
CreateEmp()
elif choice==2:
display()
elif choice==3:
Show_Salesmen()
elif choice==4:
exit()
else:
Page 6 of 15
print("Invalid Choice ! ")
main()
Page 7 of 15
Set 4
Write a menu driven program to perform the following operations on a text file “poem.txt”.
i. Write a user defined function to create a file “Poem.txt”
ii. Write a function to display the text file content.
iii. Writ e a function WCount() to count the number of words present in the text file
“Poem.txt”.
If the file contains:
I have a tree, a Green, green tree to shade me from the Sun.
Then the output should be,
Count of words = 14
iv. Exit
Page 8 of 15
c+=1
print("Count of words:",c)
def main():
while True:
print("-------MAIN MENU-----")
print("1. Create File ")
print("2. Display File")
print("3. Count and Display the number of words in the file")
print("4. Exit")
print("---------------------")
choice=int(input("Enter your choice (1..5) : "))
if choice==1:
create_file1()
elif choice==2:
display_file1()
elif choice==3:
wcount()
elif choice==4:
exit()
else:
print("Invalid Choice ! ")
main()
Page 9 of 15
Set 5
Write a menu driven program to perform the following operations on a binary file “Book.dat”
has the structure {BookNo, Book_Name, Author, Price].
i. Write a user defined function CreateFile() to input data for a record and add to
“Book.dat”.
ii. Write a function CountRect() which accepts the author’s name as parameter and count
and return the number of books written by the author. Also display the book details
written by the same author.
iii. Display the file.
iv. Exit
import pickle
def CreateBook():
n=int(input("Enter number of Books: "))
f1=open("Book.dat","wb")
for i in range(n):
BookNo=input("Enter Book No: ")
Book_Name=input("Enter Book Name: ")
Author=input("Enter Author: ")
Price=int(input("Enter Price: "))
rec=[BookNo, Book_Name, Author, Price]
pickle.dump(rec,f1)
f1.close()
def CountRec(Author):
f2=open("Book.dat","rb")
print("Displaying Books by Author : ",Author)
found=0
count=0
try:
while True:
R=pickle.load(f2)
if R[2]==Author:
print("Book No: ",R[0])
print("Book Name: ", R[1])
print("Author ", R[2])
print("Price ", R[3])
count+=1
found=1
except:
f2.close()
Page 10 of 15
if found==0:
print("No record found !")
else:
print("There are : ", count," books by Author ", Author)
def display():
print("Contents in a Binary file ")
f2=open("Book.dat","rb")
try:
print("BNo \t Name \t\t\t Author \t Price")
while True:
R=pickle.load(f2)
print(R[0],'\t',R[1],'\t',R[2],'\t',R[3])
except:
f2.close()
def main():
while True:
print("-------MAIN MENU-----")
print("1. Create File ")
print("2. Display File")
print("3. Counting & Displaying Books by Author")
print("4. Exit")
print("---------------------")
choice=int(input("Enter your choice (1..4) : "))
if choice==1:
CreateBook()
elif choice==2:
display()
#display2()
elif choice==3:
Author=input("Enter name Author name for counting books : ")
CountRec(Author)
elif choice==4:
exit()
else:
print("Invalid Choice ! ")
main()
Page 11 of 15
Set 8
Write a menu driven program to perform the following operations on a binary file “STORE.DAT”
has structure [ITEM_ID, ITEM_NAME, QTY, PRICE].
i. Write a user defined function CREATE_REC() to input data and add to file “STORE.DAT”
ii. Write a function DISPLAY_REC() to display the details of items which have the quantity
more than 35 from the file “STORE.DAT”.
iii. Display the file content.
iv. Exit
# Part i)
import pickle
def create_rec():
file=open("STORE.DAT",'wb')
n=int(input("Enter the number of Items : "))
for i in range(n):
ITEM_ID=int(input("Enter Item ID :"))
ITEM_NAME=input("Enter Item name:")
QTY=int(input("Enter QTY:"))
PRICE=int(input("Enter PRICE:"))
rec=[ITEM_ID, ITEM_NAME, QTY,PRICE]
pickle.dump(rec,file)
print("Record Saved !")
file.close()
# Part ii)
import pickle
def Display_Rec() :
file=open("STORE.DAT",'rb')
count=0
print("Displaying the details of items \
having quantity is more than 35 ")
try:
while True:
R=pickle.load(file)
#print(R)
count+=1
if R[2]>35:
print("Item_id: ",R[0])
Page 12 of 15
print("Item_name: ",R[1])
print("Quantuty: ",R[2])
print("Price: ",R[3])
except:
file.close()
return count
def display() :
print("Contents in a Binary file ")
f=open("STORE.DAT",'rb')
try:
print("ID\t Name\t Qty \t Price")
while True:
R=pickle.load(f)
print(R[0],'\t',R[1],'\t',R[2],'\t',R[3])
#print(R)
except Exception:
f.close()
def main():
while True:
print("-------MAIN MENU-----")
print("1. Create File ")
print("2. Display File")
print("3. Display Items where quantity is more than 35")
print("4. Exit")
print("---------------------")
choice=int(input("Enter your choice (1..4) : "))
if choice==1:
create_rec()
elif choice==2:
display()
elif choice==3:
print("The number of items : ",Display_Rec())
elif choice==4:
exit()
else:
print("Invalid Choice ! ")
main()
Page 13 of 15
Set 10
Write a menu driven program to perform the following operations on a text file.
i. Write a user defined function to create a text file LINE.TXT.
ii. Write a function to display the file content.
iii. Write a function PMCount() to count the number of times the letter P and M(including
the small case of ‘p’ and ‘m’) occurs in the file content.
If the file contains:
I am student.
I like programming.
Then the output should be:
Count of P/p: 1
Count of M/m: 3
iv. Exit
Page 14 of 15
print('Count of M/m:', cm)
def wcount_more_than_4_char():
f=open("LINE.txt","r")
s=f.read()
c=0
word=s.split()
num=len(word)
print("Total Number of words=",num)
print("Displaying words having more than 4 characters: ")
for i in word:
if len(i)>4:
print(i)
c+=1
print("Number of words having more than 4 characters:",c)
def main():
while True:
print("-------MAIN MENU-----")
print("1. Create File ")
print("2. Display File")
print("3. Count two characters P and M in the file")
print("4. Exit")
print("---------------------")
choice=int(input("Enter your choice (1..4) : "))
if choice==1:
create_file()
elif choice==2:
display_file()
elif choice==3:
PMCount()
elif choice==4:
exit()
else:
print("Invalid Choice ! ")
main()
Page 15 of 15