DELHI PUBLIC SCHOOL, KANPUR
P
Class XII [2025-26]
Computer Science (083)
Worksheet-File Handling
c. complete the missing statement d. complete the missing statement
import pickle import pickle
myfile = open("test.dat","wb") myfile = open("test.dat","rb")
d={1:100,2:200,3:300} d={1:100,2:200,3:300}
#statement to store dictionary d in file # statement to load dictionary data from file to “d”
myfile.close() myfile.close()
e.Write a function COUNT() in Python to read from a text f.Write a function Start_with_I() in Python, which should
file 'Gratitude.txt' and display the count of the letter 'e' in read a text file 'Gratitude.txt' and then display lines
each line Example: If the file content is as follows: starting with 'I'. Example: If the file content is as follows:
Gratitude is a humble heart's radiant glow, A timeless gift Gratitude is a humble heart's radiant glow
that nurtures and bestows. It's the appreciation for the love A timeless gift that nurtures and bestows. It's the
we're shown, In moments big and small, it's truly known. appreciation for the love we're shown, In moments big
and small, it's truly known.
The COUNT() function should display the output as:
Line 1 : 3 Then the output should be It's the appreciation for the
Line 2 : 4 love we're shown, In moments big and small, it's truly
Line 3 : 6 known.
Line 4 : 1
g.Write a function in Python to read a text file, Alpha.txt
and displays those lines which begin with the word ‘You’. h.WAP remove _lowercase() that accepts two filenames and
copy all the lines that do not start with lowercase letter from
the first file into second.
Write a function, vowelCount() in Python that counts and
displays the number of vowels in the text file named
Poem.txt
i. j.Write a Python function that displays all the words
Write a Python function that displays all the lines starting and ending with a vowel from a text file
containing the word 'vote' from a text file "Elections.txt". "Report.txt". The consecutive words should be
For example, if the file contains: separated by a space in the output.
In an election many people vote to choose their For example, if the file contains:
representative. Once there was a wise man in a village.
The candidate getting the maximum share of votes stands He was an awesome story-teller.
elected. He was able to keep people anchored while listening to
Normally, one person has to vote once. him.
Then the output should be: Then the output should be:
In an election many people vote to choose their Once a a awesome able
representative.
Normally, one person has to vote once.
9. Consider the following Binary file “Emp.txt‟, Write a function RECSHOW() to display only those records who
are earning more than 7000
SOLUTION
1.#To count no. of vowels 2. .#To count no. of characters,spaces,dots
f=open("xyz.txt") f=open("xyz.txt")
d=f.read() d=f.read()
c=0 print(d)
for i in d: c=s=v=0
if i in"aeiou": for i in d:
c=c+1 if i==" ":
print("No. of vowels",c) s=s+1
f.close() if i==".":
v+=1
c=c+1
print("No. of characters",c-s)
print("No. of spaces",s)
print("No. of dots",v)
f.close()
3.f=open("xyz.txt") 4.f=open("xyz.txt")
d=f.read() d=f.read()
k=d.split() k=d.split()
print(k) print(k)
c=0 c=0
for i in k: for i in k:
if len(i)>5: if i[-1]=='y':
c=c+1 c=c+1
print("No. of words",c) print("No. of words end with 'Y'",c)
f.close() f.close()
5.1st method 6. f=open("xyz.txt")
f=open(r"d:\demo.txt",'r') f1=open("abc.txt",'w+')
d=f.readlines() d=f.read()
print("Total no. of lines",len(d)) k=d.split()
f.close() for i in k:
2nd method if i!="Dumpty":
f=open("xyz.txt") f1.write(i)
d=f.read() f1.seek(0)
k=d.split("\n") print("After Dumpty removed\n",f1.read())
print(k) f.close()
print("Total no. of lines",len(k)) f1.close()
f.close()
7.f=open("xyz.txt") 8. # Length of each line and display long line
d=f.read() f=open("a.txt",'w+')
s=c=0 line='Welcome to\n python myxyz.in\n Regularly visit
for i in d: python myxyz.in'
if i.islower(): f.write(line)
c+=1 f.seek(0)
if i.isupper(): s=f.readlines()
s+=1 print(s)
m=0
print("Total lower characters",c) for i in s:
print("Total upper characters",s) print(“Length of each line,len(i))
f.close() if m<len(i):
m=len(i)
print("Maximum length of line" ,i, m)
9. # Display longest word
f=open("a.txt",'w+')
line='Welcome to\n python . myxyz . in\nRegularly visit
python myxyz.in'
f.write(line)
f.seek(0)
s=f.read()
s=s.split()
m=0
for i in s:
print(i)
if m<len(i):
m=len(i)
print("length of longest word",i,m)
f = open("xyz.txt",'rb+')
print(f.read(5))
print(f.tell())
print(f.read())
print(f.tell())
f.seek(7,0) # moves to 7th position from begining
print(f.read(5))
f.seek(5, 1) # moves to 5th position from current location
print(f.read(5))
f.seek(-10, 2) # Go to the 10th byte before the end
print(f.read(5))
f.close()
b'Humpt'
5
b"y Dumpty sat on a wall,\r\nHumpty Dumpty had a great fall.\r\nAll the king\
x92s horses and all the king's men\r\nCould not put Humpty together again."
145
b'Dumpt'
b' on a'
b'her a'
Write the output of following statements – Considering the content stored in file
f = open("xyz.txt ") “WORLDCUP.TXT”, write the output
str1 = ____________ # to read first line of file India won the Cricket world cup of 1983
str2 = ___________ # to read next line of file f = open(“WORLDCUP.TXT”)
str3 = ___________ # to read remaining lines of file print(f.read(2)) In
str1 = f.readline() print(f.read(2)) di
str2 = f.readline() print(f.read(4)) a wo
str3 = f.readlines() OR str3 = f.read()
Write a function COUNT() in Python to read from a text file Write a function Start_with_I() in Python, which should
'Gratitude.txt' and display the count of the letter 'e' in each read a text file 'Gratitude.txt' and then display lines
line Example: If the file content is as follows: starting with 'I'. Example: If the file content is as follows:
Gratitude is a humble heart's radiant glow, A timeless gift Gratitude is a humble heart's radiant glow
that nurtures and bestows. It's the appreciation for the love A timeless gift that nurtures and bestows. It's the
we're shown, In moments big and small, it's truly known. appreciation for the love we're shown, In moments big
and small, it's truly known.
The COUNT() function should display the output as:
Line 1 : 3
Line 2 : 4 Then the output should be It's the appreciation for the
Line 3 : 6 love we're shown, In moments big and small, it's truly
Line 4 : 1 known.
def Count(): def Start_with_I():
F=open('Gratitude.txt') F=open('Gratitude.txt')
T=F.readlines() T=F.readlines()
X=0 for i in T:
for i in T: if i[0] in 'Ii':
print('Line',X,':',i.count('e')) print(i,end='')
X=X+1 F.close()
F.close()
Start_with_I()
Write a function in Python to read a text file, Alpha.txt and Write a function, vowelCount() in Python that counts and
displays those lines which begin with the word ‘You’. displays the number of vowels in the text file named
Poem.txt
Write a Python function that displays all the lines containing Write a Python function that displays all the words
the word 'vote' from a text file "Elections.txt". For example, starting and ending with a vowel from a text file
if the file contains: "Report.txt". The consecutive words should be
In an election many people vote to choose their separated by a space in the output.
representative. For example, if the file contains:
The candidate getting the maximum share of votes stands Once there was a wise man in a village.
elected. He was an awesome story-teller.
Normally, one person has to vote once. He was able to keep people anchored while listening to
Then the output should be: him.
In an election many people vote to choose their Then the output should be:
representative. Once a a awesome able
Normally, one person has to vote once.
complete the missing statement complete the missing statement
import pickle import pickle
myfile = open("test.dat","wb") myfile = open("test.dat","rb")
d={1:100,2:200,3:300} d={1:100,2:200,3:300}
#statement to store dictionary d in file # statement to load dictionary data from file
myfile.close() to “d”
pickle.dump(d,myfile) myfile.close()
pickle.load(myfile)
Consider the following Binary file „Emp.txt‟, Write a function RECSHOW() to display only those records
who are earning more than 7000
import pickle
def RECSHOW():
emp=[]
f = open('employee.dat','rb')
while True:
try:
emp = pickle.load(f) # loading data in emp list except
EOFError:
break
print("%10s"%"EMP NO ","%20s"%"EMP NAME ","%10s"%"EMP SALARY")
print("*****************************************************")
for e in emp:
if (e[2]>7000):
print("%10s"%e[0],"%20s"%e[1],"%10s"%e[2])
found=True
if found==False:
print("## SORRY EMPLOYEE NUMBER NOT FOUND ##")
f.close()