CLASS –12 | SUBJECT – Computer Science
Ch. 4- Worksheet (Text file)
Q1. Write a program to read entire content of text file. (“[Link]”)
f=open("[Link]","r")
data=[Link]()
print(data)
[Link]()
Q2. Write a program to read first 5 characters from the text file (“[Link]”)
f=open("[Link]","r")
data=[Link](5)
print(data)
[Link]()
Q3. Write a program to read first line from the text file (“[Link]”)
f=open("[Link]","r")
data=[Link]()
print(data)
[Link]()
Q4. Write a program to display number of lines in a text file (“[Link]”).
f=open("[Link]","r")
list=[Link]()
print(list)
s=len(list)
print(s)
[Link]()
Q5. Write a program to display first line from the text file (“[Link]”) using readlines().
f=open("[Link]","r")
list=[Link]()
for i in list:
print(i)
break
[Link]()
Q6. Write a program in python to display first character of all the lines from the text file(“[Link]”).
f=open("[Link]","r")
list=[Link]()
for i in list:
print(i[0])
[Link]()
Q7. Write a program in python to display all the lines from the text file (“[Link]”) with first character in
uppercase.
f=open("[Link]","r")
list=[Link]()
for i in list:
print([Link]())
[Link]()
Q8. Write a program in python to find the number of characters in a text file (“[Link]”).
f=open("[Link]","r")
a=[Link]()
print(len(a))
[Link]()
Q9. Write a program to write following data in a file “[Link]”.
We are learning Python
f=open("[Link]","w")
[Link]("we are learning python")
[Link]()
Q10. Write a program to read data from “[Link]” and write in another file “[Link]”.
f=open("[Link]","r")
a=[Link]()
[Link]()
f=open("[Link]","w")
[Link](a)
[Link]()
Q11: Write a program to read data from [Link] and then write “Hello world” in same file.
f=open("[Link]","r+")
a=[Link]()
print(a)
[Link]("Hello world")
[Link]()
Q12: Write a program to write text “Hello world” in [Link] and then read that data from same file.
f=open("[Link]","w+")
a=[Link]("Hello world")
[Link](0)
a=[Link]()
print(a)
[Link]()
Q13: Write a program to read the content from file “[Link]” and write to same file again after changing the
case. (Using swapcase())
f=open("[Link]","r+")
a=[Link]()
s=[Link]()
print(s)
[Link](0)
[Link](s)
[Link]()
Q14. Write a program to read a file “[Link]” and replace word “school” by “college” and write it again in same
file. (Using replace (old,new) )
f=open("[Link]","r+")
a=[Link]()
s=[Link]("School","College")
print(s)
[Link](0)
[Link](s)
[Link]()
Worksheet 2 (PYQs) (Text file)
Q1. Write a function in Python that counts the number of “Me” or “My” (in smaller case also) words present in
a text file “[Link]”. If the “[Link]” contents are as follows:
My first book was Me and My Family. It gave me chance to be Known to the world.
The output of the function should be: Count of Me/My in file: 4
def countmemy():
c=0
f=open("[Link]","r")
data=[Link]()
l=[Link]()
for i in l:
if [Link]() in "me my":
c=c+1
print("No of me and my is:",c)
countmemy()
Q2. Write a function AMCount() in Python, which should read each character of a text file [Link], should
count and display the occurence of alphabets ‘A’ and ‘M’ (including small cases ‘a’ and ‘m ‘too). Example: If the
file content is as follows:
Updated information As simplified by official websites.
The AMCount() function should display the output as: A or a = 4, M or m =2
def AMcount():
A=0
M=0
f=open("[Link]","r")
data=[Link]()
for i in data:
if [Link]()=="a":
A=A+1
elif [Link]()=="m":
M=M+1
print("No. of A or a is:",A)
print("No. of M or m is:",M)
AMcount()
Q3. Write a function in python to count the number of lines in a text file ‘ [Link]’ which is starting with an
alphabet ‘A’ .
def Acount():
A=0
f=open("[Link]","r")
data=[Link]()
for i in data:
if i[0]=="A":
A=A+1
print("No. of line started with A is :",A)
Acount()
Q4. Write a method/function DISPLAYWORDS() in python to read lines from a text file [Link], and display
those words, which are less than 4 characters.
def displaywords():
A=0
f=open("[Link]","r")
data=[Link]()
L=[Link]()
for i in L:
if len(i)<4:
print(i)
displaywords()
Q5. Write a function RevText() to read a text file “ [Link] “ and Print only word starting with ‘I’ in reverse
order . Example: If value in text file is: INDIA IS MY COUNTRY Output will be: AIDNI SI MY COUNTRY.
def RevText():
f=open("[Link]","r")
data=[Link]()
L=[Link]()
print(L)
for i in L:
if i[0]=="I":
print(i[::-1],end=" ")
else:
print(i,end=" ")
RevText()
Q6. Write a function in python to count the number of lowercase alphabets present in a text file “[Link]”
def Lcount():
f=open("[Link]","r")
data=[Link]()
for i in data:
if [Link]():
c=c+1
print("No of lower case alphabets are:",c)
Lcount()
Q7. Write a user-defined function named count() that will read the contents of text file named “[Link]” and
count the number of lines which starts with either “I‟ or “M‟. E.g. In the following paragraph, there are 2 lines
starting with “I‟ or “M‟:
India is the fastest growing economy.
India is looking for more investments around the globe.
The whole world is looking at India as a great market.
Most of the Indians can foresee the heights that India is capable of reaching.
def count():
ci=0
cm=0
f=open("[Link]","r")
data=[Link]()
for i in data:
if i[0]=="I":
ci=ci+1
elif i[0]=="M":
cm=cm+1
print("No of line starts with L are:",ci)
print("No of line starts with M are:",cm)
count()
Q8. Write a user defined function countwords() in python to count how many words are present in a text file
named “[Link]”. For example, if the file [Link] contains following text:
Co-education system is necessary for a balanced society. With co-education system, Girls and Boys may
develop a feeling of mutual respect towards each other.
def countwords():
f=open("[Link]","r")
data=[Link]()
L=[Link]()
print(len(L))
countwords()
Q9. Write a user defined function in Python that displays the number of lines starting with ‘H’ in the file
[Link]. Eg: if the file contains:
Whose woods these are I think I know.
His house is in the village though;
He will not see me stopping here
To watch his woods fill up with snow.
def countH():
c=0
f=open("[Link]","r")
data=[Link]()
for i in data:
if i[0]=="H":
c=c+1
print("No. of lines starts with H are:",c)
countH()
Q10. Write a method COUNTLINES() in Python to read lines from text file ‘[Link]’ and display the lines
which are not starting with any vowel.
Example: If the file content is as follows:
An apple a day keeps the doctor away.
We all pray for everyone’s safety.
A marked difference will come in our country.
The COUNTLINES() function should display the output as:
The number of lines not starting with any vowel – 1
def COUNTLINES():
c=0
f=open("[Link]’ ","r")
data=[Link]()
print(data)
for i in data:
if i[0] not in "AEIOUaeiou":
print(i)
c=c+1
print("No. of lines not staring with any vowel:",c)
COUNTLINES()
Q11. Write a function ETCount() in Python, which should read each character of a text file “[Link]” and
then count and display the count of occurrence of alphabets E and T individually (including small cases e and t
too).
Example: If the file content is as follows:
Today is a pleasant day.
It might rain today.
It is mentioned on weather sites
The ETCount() function should display the output as:
E or e: 6
T or t : 9
def ETCount():
e=0
t=0
f=open("[Link]","r")
data=[Link]()
for i in data:
if [Link]()=="e":
e=e+1
elif [Link]()=="t":
t=t+1
print("No. of t are:",t)
print("No. of e are:",e)
ETCount()