OMSAKTHI
G B PUBLIC SCHOOL
CLASS XII-COMPUTER SCIENCE
TEXT FILES
1. A text file named IONS.TXT contains some text. Write an user-defined function
ShowP() which should display all such words of the file which start with alphabet
'P' and has the substring ‘íon’ in the word
For example : If the file IONS.TXT contains :
"Whether an atom will form a cation or an anion is based on its
position in the periodic table is not a presumption"
Then the function ShowP() should display the output as
position
presumption
def ShowP():
f=open('IONS.txt','r')
rec=f.read()
for i in rec.split():
if i[0]in 'Pp' and 'ion' in i:
print(i)
ShowP()
2. Write a function in python to count the number lines in a text file ‘Country.txt’ which
is starting with an alphabet ‘W’ or ‘H’, and also check for 3 letter word, If the file
contents are as follows:
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.
The output of the function should be:
W or w : 1
H or h : 2
The number of 3 letter words are:6
def display():
c=0
c1=0
c2=0
f=open('country.txt','r')
rec=f.readlines()
for i in rec:
if i[0] in 'Ww':
c+=1
if i[0] in 'Hh':
c1+=1
word=i.split()
for i in word:
if len(i)==3:
c2+=1
print("W or w :",c)
print("H or h : ",c1)
print("The number of 3 letter word are:”,c2)
3. Write a function in Python to replace every occurrence of the uppercase alphabet in
the text file ‘Sample.txt’ with its equivalent lowercase alphabet and also returns the
number of replacements done in the file.
def display():
s=''
c=0
f=open('sampl1.txt','r')
rec=f.read()
for i in rec:
if i.isupper():
s+=i.lower()
c+=1
continue
s+=i
print(s)
print("no of replacement",c)
display()
4. Write a method COUNTLINES() in Python to read lines from text file‘TESTFILE.TXT’
and display the lines which are starting with any article (a,an, the) insensitive of the
case.
Example: If the file content is as follows:
Give what you want to get.
We all pray for everyone’s safety.
A marked difference will come in our country.
The Prime Minister is doing amazing things.
The COUNTLINES() function should display the output as:
The number of lines starting with any article are : 2
def COUNTLINES():
c=0
f=open('TESTFILE.TXT','r')
rec=f.readlines()
for i in rec:
word=i.split()
if word[0].lower() in ['a','an','the']:
c+=1
print("The number of lines starting with any article are :",c)
COUNTLINES()
5. A text file Message.txt exists with several lines of text. Write a function CREATE()
create SMS.txt to have first 30 characters of each line; display contents of the new file
def CREATE():
s=''
f=open('Message.txt','r')
f1=open('SMS.txt','w+')
rec=f.readlines()
for i in rec:
s=i[0:30]
f1.write(s)
f1.write('\n')
f.close()
f1.close()
f2=open('SMS.txt','r')
data=f2.read()
print(data)
CREATE()
6. Write a method Mod_line(word) in Python to read the contents from the text file
“Sample.txt” and change all the lines that contains the word passed as an argument to
the function to Uppercase form.
Example:
If the content of Sample.txt is as follows:
Never win people with arguments, rather defeat them with your smile.
People who always wish to argue with you, cannot bear your silence.
Keep smiling always.
The word to search in the file is ‘people’ , the expected file content
is:
NEVER WIN PEOPLE WITH ARGUMENTS, RATHER DEFEAT
THEM WITH YOUR SMILE.PEOPLE WHO ALWAYS WISH TO
ARGUE WITH YOU, CANNOT BEAR YOUR SILENCE. Keep smiling
always.
word='people'
def Mod_line(word):
f=open('Sample.txt','r')
rec=f.readlines()
for i in rec:
if word.lower() in i:
print(i.upper())
Mod_line(word)
7. Write an user defined function Odd_Len( ) which takes as argument a text file that
already exists. The function should return the number of words that have odd length
and start with an uppercase letter.
(eg) if the content of the file is:
Look before you leap
All that glitters is not gold
Honesty is the best policy
The output should be: 2
[as the odd length words starting with uppercase words are: All, Honesty
def Odd_Len():
f=open('Sample.txt','r')
rec=f.read().split()
for i in rec:
if len(i)%2!=0 and i[0].isupper():
print(i)
Odd_Len()
8. Write an user defined function AI_Count() that reads from an existing file “Quotes.txt”
one character at a time and prints the number of occurances of ‘a’ and ‘i’ in both the
cases, in the format shown below:
(eg) if the content of the file is:
Look before you leap
All that glitters is not gold
Honesty is the best policy
Output should be in the format:
Count of 'a'= 3
Count of 'i'= 4
def AI_Count():
c=0
c1=0
f=open('Quotes.txt','r')
rec=f.read()
for i in rec:
if i=='a' or i=='a':
c+=1
if i=='i' or i=='I':
c1+=1
print('Count of a:',c)
print('Count of i"',c1)
AI_Count()
9. Ankush is trying to write a function countmemy( ) in Python that counts the number
of “Me” or “My” (in smaller case also) words present in a text file “Story.txt”. He has
written an incomplete code. Help him to complete the code, Consider the following
text to be the content of “Story.txt” file:
My first book was Me and My Family. It gave me a chance to be known to the world.
____def_____________ countmemy(): #Statement1
File_obj=open("_Story.txt","r") #Statement2
d = File_obj.read() #Statement3
m = d.split() #Statement4
c=0
for i in m: #Statement5
if i in ["me","my"]: #Statement6
c = c+1
print("Count is ", c)
countmemy()
10. Write a function COUNT() in Python to read from a text file 'Gratitude.txt' and
display the count of the letter 'e' in each line.
Example: If the file content is as follows:
Gratitude is a humble heart's radiant glow,
A timeless gift that nurtures and bestows.
It's the 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
Line 3 : 6
Line 4 : 1
def COUNT():
f=open("Gratitute.txt","r")
rec=f.readlines()
s=0
for i in range(len(rec)):
print("Line",i+1,rec[i].count('e'))
COUNT()