0% found this document useful (0 votes)
11 views12 pages

Screenshot 2025-02-27 at 8.59.35 AM

The document contains Python code snippets for various functions that handle binary files using the pickle module. These functions include creating files, counting records based on specific criteria, adding records, searching for products, modifying data, and displaying records from different datasets like books, students, buses, sports, salaries, and toys. Each function demonstrates how to read from and write to binary files while performing specific operations based on user input.

Uploaded by

harishmanoharpm
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)
11 views12 pages

Screenshot 2025-02-27 at 8.59.35 AM

The document contains Python code snippets for various functions that handle binary files using the pickle module. These functions include creating files, counting records based on specific criteria, adding records, searching for products, modifying data, and displaying records from different datasets like books, students, buses, sports, salaries, and toys. Each function demonstrates how to read from and write to binary files while performing specific operations based on user input.

Uploaded by

harishmanoharpm
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/ 12

import pickle

def createfile():
fobj=open("Book.dat","wb")
BookNo=int(input("Enter Book Number : "))
Book_name=input("Enter book Name :")
Author = input("Enter Author name: ")
Price = int(input("Price of book : "))
rec=[BookNo, Book_name ,Author, Price]
pickle.dump(rec, fobj)
fobj.close()
def countrec(Author):
fobj=open("Book.dat", "rb")
num = 0
try:
while True:
rec=pickle.load(fobj)
if Author==rec[2]:
num = num + 1
print(rec[0],rec[1],rec[2],rec[3])
except:
fobj.close()
return num
createfile()
n=countrec("amit")
print("Total records", n)
import pickle
def countrec():
fobj=open("student.dat","rb")
num = 0
try:
while True:
rec=pickle.load(fobj)
if rec[2]>75:
num = num + 1
print(rec[0],rec[1],rec[2])
except:
fobj.close()
return num
n=countrec()
print("Total records", n)
import pickle
def countrec():
fobj=open("bus.dat","rb")
num = 0
try:
while True:
rec=pickle.load(fobj)
if rec[2]=="Cochin" or rec[2]=="cochin":
num = num + 1
print(rec[0],rec[1],rec[2])
except:
fobj.close()
return num
n=countrec()
print(n)
import pickle
def addrec():
fobj=open("student.dat","ab")
rollno=int(input("Roll Number : "))
sname=input("Student Name :")
rec=[rollno,sname]
pickle.dump(rec,fobj)
fobj.close()
addrec()

import pickle
def searchprod(pc):
fobj=open("product.dat","rb")
num = 0
try:
while True:
rec=pickle.load(fobj)
if rec[0]==pc:
print(rec)
except:
fobj.close()
pc=int(input("enter product code"))
rec=searchprod(pc)
print(rec)
6. Write a function routechange(route number) which takes the Route number as parameter
and modify the route name(Accept it from the user) of passed route number in a binary file
“route.dat”.
import pickle
def routechange(rno):f
obj=open("route.dat","rb")
try:
while True:
rec=pickle.load(fobj)
if rec[0]==rno:
rn=input("Enter route name to be changed ")
rec[1]=rn
print(rec)
except:
fobj.close()
routechange(1)
7.Write a function countrec(sport name) in Python which accepts the nameof sport as
parameter and count and display the coach name of a sport whichis passed as argument from
the binary file “sport.dat”. Structure of record in afile is given below
[sport name, coach name]
import pickle
def countrec(sn):
num=0
fobj=open("data.dat","rb")
try:
print("Sport Name","\t","Coach Name")
while True:
rec=pickle.load(fobj)
if rec[0]==sn:
print(rec[0],"\t\t",rec[1])
num=num+1
return num
except:
fobj.close()
8. A binary file “salary.DAT” has structure [employee id, employee name, salary]. Write a
function countrec() in Python that would read contents of the file “salary.DAT” and display
the details of those employee whose salary is above20000.
import pickle
def countrec():
num=0
fobj=open("data.dat","rb")
try:
print("Emp id\tEmp Name\tEmp Sal")
while True:
rec=pickle.load(fobj)
if rec[2]>20000:
print(rec[0],"\t\t",rec[1],"\t\t",rec[2])
except:
fobj.close()
countrec()
9.Amit is a monitor of class XII-A and he stored the record of all the studentsof his class ina
file named “class.dat”. Structure of record is [roll number, name,percentage]. His computer
teacher has assigned the following duty to Amit.Write a function remcount( ) to count the
number of students who need remedialclass (student who scored less than 40 percent)

def remcount():
fobj=open("class.dat","rb")
try:
print("Roll No\t Name\t Percentage")
while True:
rec=pickle.load(fobj)
if rec[2]>40:
print(rec[0],"\t\t",rec[1],"\t\t",rec[2])
except:
fobj.close()
countrec()
10.A binary file “emp.dat” has structure [employee id, employee name]. Writea function
delrec(employee number) in Python that would read contents of thefile “emp.dat” and delete
the details of those employee whose employee numberis passed as argument.
11. Write a function countrec() in Python that would read contents of the file
"STUDENT.DAT" and display the details of those students whose percentage is above 75.
Also display number of students scoring above 75%
Ans:
import pickle
def countrecw():
f= open('STUDENT.DAT','ab')
rec = [['Admission_number', 'Name', 'Percentage']]
ans ='y'
while ans.lower() == 'y':
a_no=int(input('Enter Admission number: '))
name=input('Enter Name: ')
percent=int(input('Enter Percentage: '))
r = rec.append([a_no, name, percent])
ans = input('continue ? (y/n): ')
print(r)
pickle.dump(rec,f)
f.close()
def countrec():
f = open('STUDENT.DAT','rb')
records = []
c =0
while True:
try:
records = pickle.load(f)
#print(records)
for line in records:
if line[2] == 'Percentage':
pass
elif line[2] >= 75:
c+=1
print(line)
except:print('Number of students scoring above 75% : ' , c)
break
countrecw()
countrec()
12. A binary file "Toy.dat" has structure [TID, Toy, Status, MRP].i. Write a user defined
function CreateFile() to input data for a record and add to"Toy.dat"ii. Write a function
OnOffer() in Python to display the detail of those Toys, whichhas status as "ON OFFER"
from "Toy.dat" file.
import pickle
def CreateFile():
f = open('Toy.dat','wb')
rec = [['TID','Toy','Status','MRP']]
ans ='y'
while ans.lower() == 'y':
TID=int(input('Enter Toy ID: '))
Toy=input('Enter Toy Name: ')
Status=input('Enter Status: ')
MRP=int(input('Enter MRP: '))
rec.append([TID,Toy,Status,MRP])
ans = input('continue ? (y/n): ')
print(rec)
pickle.dump(rec,f)
f.close()
CreateFile()
def OnOffer():
f = open('Toy.dat','rb')
records = []
c =0
while True:
try:
records = pickle.load(f)#print(records
for line in records:
if line[2] == "ON OFFER":
print(line)
except:
break
OnOffer()

13.Considering the following definition of dictionary MULTIPLEX, write a methodin


python to search and display all the content in a pickled fileCINEMA.DAT,where MTYPE
key of the dictionary is matching with the value"Comedy".
MULTIPLEX = {'MNO': _____, 'MNAME": _____, 'MTYPE': _____}
import pickle
def CreateFile():
f = open ('CINEMA.DAT','wb')
rec = []
ans = 'y'
while ans.lower()=='y':
MNO = int(input('Enter MNO: '))
MNAME = input('Enter MNAME: ')
MTYPE = input('Enter MTYPE: ')
MULTIPLEX = {'MNO':MNO,'MNAME':MNAME,'MTYPE':MTYPE}
rec.append(MULTIPLEX)
ans = input("continue?(y/n):")
print(rec)
pickle.dump(rec,f)
f.close()
CreateFile()
def readFile():
f = open ('CINEMA.DAT','rb')
nested_records = []
while True:
try:
nested_records = pickle.load(f)
print(nested_records)
for d in nested_records:
if d['MTYPE'] == "Comedy":
print (d)
except :
break
readFile()

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy