Dav Public School, Pokhaiput, Bhubaneswar Chapter Test-2024 File Handling Class-Xii Date:-12.07.24 Q1
Dav Public School, Pokhaiput, Bhubaneswar Chapter Test-2024 File Handling Class-Xii Date:-12.07.24 Q1
CHAPTER TEST-2024
FILE HANDLING
CLASS-XII
Date:-12.07.24
Q1.Write a function that would read contents from file sports.dat and creates a file named Atheletic.txt
copying only those records from sports.txt where the event name is " Atheletics ".
A file sports.txt contains information in following format :Event ~ Participant
Q2.A file contains a list of telephone numbers in the following form: The names contain only one word the
names and telephone numbers are separated by white spaces Write program to read a file and display its
contents in two columns.
Q4. Write a program to count the number of upper- case alphabets present in a text file "Article.txt".
Q5 .Write a method/function DISPLAYWORDS() in python to read lines from a text file STORY.TXT,
and display those words, which are less than 4 characters.
Q6. Write a function in Python to count and display the number of lines starting with alphabet 'A' present
in a text file " LINES.TXT". e.g., the file "LINES.TXT" contains the following lines:
Q7. Consider the following definition of dictionary Staff, write a method in python to search and display
the content in a pickled file staff.dat, where Staffcode key of the dictionary is matching with 'S0105'.
Q9. A binary file “Employee.dat” has structure (Emp_number, Name, Salary) Write a function detailrec() in
Python that would read contents Of the file “Employee.dat” and display the details of those employees whose
Salary is above 3000. Also display number of employees getting salary above 3000.
Q10.A binary file “Shop.dat” has structure [ShopNo, Shop_Name, Owner, Cost].
i)Write an user defined function CreateFile() to input data for a record and add to Shop.dat.
ii)Write a function CountRec(Owner) in Python which accepts the Ownername as parameter and count
and return number of Shops by the given Ownerare stored in the binary file “Shop.dat”
**********************************************************************************
Q1
def portal( ) :
file1 = open("sports.txt","r")
file2 = open("Atheletics.txt","w")
lst = file1.readlines()
for i in lst :
print(i [ : 9 ])
if i[ : 9 ] == "atheletic" or i [ : 9 ] == "Atheletic" :
file2.write(i)
file1.close()
file2.close()
portal()
lst = file.readlines()
for i in lst :
data = i.split()
print( data[0] ,end = "\t" )
print("|" , end = "\t")
print ( data[1] )
file.close()
Q.3 .Write a program to count the words "to" and "the" present in a text file "Poem.txt".
Q3. to_no = 0
the_no = 0
for i in lst :
word = i.split()
for j in word :
if j == "to" :
to_no += 1
elif j == "the" or j == "The" :
the_no += 1
Q4. Write a program to count the number of upper- case alphabets present in a text file
"Article.txt".
Q4.
count = 0
file = open("pathwala.txt","r")
sen = file.read()
for i in range ( len(sen) ) :
if sen[i ].isupper() :
count += 1
Q5.
def DISPLAYWORDS() :
file = open("story.txt", "r")
lst = file.readlines()
for i in lst :
word = i.split()
for j in word :
if len( j ) < 4 :
print( j )
file.close()
DISPLAYWORDS()
Q6. Write a function in Python to count and display the number of lines starting with alphabet 'A'
present in a text file " LINES.TXT". e.g., the file "LINES.TXT" contains the following lines:
Q6.
count = 0
file = open("LINES.txt","r")
lst = file.readlines()
for i in lst :
if i[ 0 ] == "A" :
print(i)
count += 1
print("So for number of sentences started with A : ",count)
file.close()
Q7. Consider the following definition of dictionary Staff, write a method in python to search and
display the content in a pickled file staff.dat, where Staffcode key of the dictionary is matching with
'S0105'.
Q7.
import pickle
file = open("staff.dat", "rb")
found = 0
try :
while True :
staff = pickle.load(file)
if staff [ "Staff Code" ] == 'S0105':
print(staff)
found=1
except EOFError :
if found == 0:
print("Not found !!!")
file.close()
Q8. Write a Python program to write a nested Python list to a csv file in one go. After writing the
CSV read the CSV file and display the content.
Answer =
data = csv.reader(file)
for i in data :
print(i)
file.close()
Q9.
import pickle defCreate():
File= open(‘CINEMA.DAT’ ,’wb’)
ans=’y’
while ans ==’y’ :
Q10.
def Search(tov):
File= open(‘COMPANY.DAT’,’rb’)
Count=0
try:
while True:
C= pickle.load(File)
if C[‘Turnover’] >tov :
Count = Count+1
except EOFError:
pass
return Count
File.close()
tov = int(input(‘Enter minimum turnover :’))
res = Search(tov)
printf(‘Companies having turnover more than the given value ’, res)
*********************************************************************