0% found this document useful (0 votes)
23 views

Dav Public School, Pokhaiput, Bhubaneswar Chapter Test-2024 File Handling Class-Xii Date:-12.07.24 Q1

wjwiwk

Uploaded by

swastik.neew
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
23 views

Dav Public School, Pokhaiput, Bhubaneswar Chapter Test-2024 File Handling Class-Xii Date:-12.07.24 Q1

wjwiwk

Uploaded by

swastik.neew
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 7

DAV PUBLIC SCHOOL,POKHAIPUT , BHUBANESWAR

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.

A file contains a list of telephone numbers in the following form:


Arvind 7258031
Sachin 7259197 .
Q3. Write a program to count the words "to" and "the" present in a text file "Poem.txt".

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:

A boy is playing there.


There is a playground.
An aeroplane is in the sky.

Alphabets & numbers are allowed in password.


The function should display the output as 3.

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'.

Staff = {‘Staff Code': _____, 'Name' = _____}


Q8. Write a function that reads a CSV file and creates another CSV file with the same content, but with a
different delimiter. Search a particular item from the new file .If found then print Search is
successful .Otherwise print Item not found .

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()

Q2.print("Name | Phone no. ")

file = open("portal.txt", "r")

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

file = open("Poem.txt", "r")


lst = file.readlines()

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

print("Number 'to' : " , to_no)


print("Number 'the' : " , the_no)
file.close()

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

print("Number of upper case alphabet : ", count)


file.close()

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.

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:

A boy is playing there.


There is a playground.
An aeroplane is in the sky.

Alphabets & numbers are allowed in password.


The function should display the output as 3.

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'.

Staff = {‘Staff Code': _____, 'Name' = _____}

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 =

Q8. import csv


file = open( "Pathwala.txt","r+" )
lst = eval(input("Enter a nested list :-"))
path_write = csv.writer( file)
path_write.writerows( lst )

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’ :

fno = int(input(“Enter Film N0. :”))


fname = input(“Enter Film Name. :”)
ftype = input(“Enter Film type. :”)
F={‘FNo’ : fno, ‘FNAME’ : fname, ‘FType’: ftype}
pickle.dump(F, File)
Ans = input(“Want to insert more records? (y/n) ;”)
printf(“File created”)
File.close()
def Search();
File = open(‘CINEMA.DAT’,’rb’)
try:
while True:
F=pickle.load(file)
if F[‘Ftype’] == “Comedy”:
print(F)
except EOFError:
pass
File.close()
Create()
Search()

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)

*********************************************************************

You might also like