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

Text Files

The document contains a series of Python programming tasks that involve reading from text files and performing various operations such as counting words, vowels, and lines based on specific criteria. Each task includes a description and a sample function implementation to achieve the desired outcome. The tasks cover a range of topics including string manipulation, file handling, and conditional logic.

Uploaded by

gangsterganeshhh
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
60 views

Text Files

The document contains a series of Python programming tasks that involve reading from text files and performing various operations such as counting words, vowels, and lines based on specific criteria. Each task includes a description and a sample function implementation to achieve the desired outcome. The tasks cover a range of topics including string manipulation, file handling, and conditional logic.

Uploaded by

gangsterganeshhh
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 16

#TEXT FILES #Q.

No 27

1. a) Write a function countINDIA() which read a text üle


8myüle.txt9 and print the frequency
of the words 'India' in it (ignoring case of the word).
Example: If the üle content is as follows:
INDIA is my country. I live in India. India has many states.
The countIndia() function should display the output as:
Frequency of India is 3

def countINDIA():
f=open('myüle.txt')
s=f.read()
l=s.split()
k=0
for i in l:
if i.upper()=='INDIA':
k=k+1
print('Frequency of India is ',k)
countINDIA()

b) Write a function countVowel() in Python, which should


read each character of a text üle
'myüle.txt' and then count and display the count of
occurrence of vowels (including small cases
and upper case).
Example:
If the üle content is as follows:
INDIA is my country. I live in India. India has many states.
The countVowel() function should display the output as:
Total number of vowels are : 20

def countVowel():
ctr=0
f=open('d:\\myüle.txt')
data=f.read()
for ch in data:
if ch.lower() in 'aeiou':
ctr=ctr+1
print('Total number of vowels are :', ctr)
countVowel()

2. a) A pre-existing text üle data.txt has some words written


in it. Write a python
function displaywords() that will print all the words that are
having length
greater than 3.
Example:
For the üe content:
A man always wants to strive higher in his life
He wants to be perfect.
The output after executing displayword() will be:
Always wants strive higher life wants perfect

def displaywords():
f = open('data.txt','r')
s = f.read()
lst = s.split()
for x in lst:
if len(x)>3:
print(x, end=" ")
f.close()
displaywords()

OR

b)
A pre-existing text üle info.txt has some text written in it.
Write a python
function countvowel() that reads the contents of the üle
and counts the
occurrence of vowels(A,E,I,O,U) in the üle.

def countvowels():
f = open('info.txt', 'r')
s = f.read()
count = 0
for x in s:
if x in 'AEIOU':
count+=1
print(count)
f.close()
countvowels()'''

3. a) Write a function COUNT_AND( ) in Python to read the


text üle "STORY.TXT" and count the
number of times "AND" occurs in the üle. (include
AND/and/And in the counting)

def COUNT_AND():
count=0
üle=open('STORY.TXT','r')
line = üle.read()
word = line.split()
for w in word:
if w in ['AND','and','And']:
count=count+1
üle.close()
print(count)
COUNT_AND()

OR

b)
Write a function DISPLAYWORDS( ) in python to display
the count of words starting with <t= or <T=
in a text üle 'STORY.TXT'.

def DISPLAYWORDS():
count=0
üle=open('STORY.TXT','r')
line = üle.read()
word = line.split()
for w in word:
if w[0]=="T" or w[0]=="t":
count=count+1
üle.close()
print(count)
DISPLAYWORDS()

4 a)Write a function in Phyton to read lines from a text üle


'visiors.bct', and
display only those lines, which are starting with an
alphabet 'P".
If the content of üle is:
Visitors from various cities
Particularly, want to visit the museum.
Looking to learn more story about countries with their
cultures.
The output should be:
Particularly, they want to visit the museum.

def rdlines():
üle = open('visitors.txt','r')
for line in üle:
if line[0] == 'P':
print(line)
üle.close()
# Call the rdlines function.
rdlines()

OR

b)
Write a method in Python to read lines from a text üle
'book.txt', to ünd and
display the occurrence or the word 'are'. For example, if
the content of the
is:
Books are referred to as a man's best friend. They are very
beneücial for
mankind and have helped it evolve. Books leave a deep
impact on us and are
responsible for uplifting our mood.
|The output should be 3.

def count_word():
üle = open('book.txt','r')
count = 0
for line in üle:
words = line.split()
for word in words:
if word == 'India':
count += 1
print(count)
üle.close()
# call the function count_word().
count_word()

#5 a)
Write a method/function DISPLAYWORDS() in python to
read lines from a text üle
STORY.TXT, and display those words, which are less than 4
characters.

def displaywords():
f = open("story.txt","r")
d = f.read()
m = d.split()
for i in m:
if (len (i) <4):
print (i)
displaywords ()

OR
b)
Write a function RevText() to read a text üle "Story.txt" and
Print only word starting
with 'I' in reverse order.
Example:
If value in text üle is:
INDIA IS MY COUNTRY
Output will be: AIDNI SI MY COUNTRY.

def revtext():
f=open("story.txt","r")
s=""
while True:
d=f.readline()
if not d:
break
else:
m=d.split()
for i in m:
if i[0]=='i' or i[0]=='I':
s=s+" "+(i[::-1])
else:
s=s+" "+i
prinit(s)
s=""
revtext()

#6 a)
Write a method COUNTLINES() in Python to read lines
from text üle
8TESTFILE.TXT9 and display the lines which are starting
with any article (a,
an, the) insensitive of the case.
Example:
If the üle content is as follows:
Give what you want to get.
We all pray for everyone9s safety.
A marked diûerence 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() :
üle = open ('TESTFILE.TXT', 'r')
List = üle.readlines()
count=0
for line in List:
words=line.split()
if words[0].lower() in (8a9,8an,8the9):
count = count + 1
print("The number of lines starting with any article are
:", count)
üle.close()

b)
Write a function GPCount() in Python, which should read
each character
of a text üle <STORY.TXT= and then count and display the
count of
occurrenceof alphabets G and P individually (including
small cases g and
p too).
Example:
If the üle content is as follows:
God helps those who help themselves.
Great way to be happy is to remain positive.
Punctuality is a great virtue.
The GPCount() function should display the output as:
The number of G or g: 3
The number of P or p : 6

def GPCount() :
üle = open ('STORY.TXT', 'r')
content = üle.read()
for i in content:
if i.lower()== 'g':
countG = countG + 1
if i.lower()== 'p':
countP = countP + 1
print ("The number of G or g : ", countG)
print ("The number of P or p : ", countP)
üle.close()

#7. a)
Suppose contentof'Myüle.txt'is
Humpty Dumpty sat on a wall
Humpty Dumpty had a great fall
All the king's horses and all the king's men
Couldn't put Humpty together again
Write a python function named RECORD to calculate total
number of characters in 8Myüle.txt9

def RECORD():
f1=open('Myüle.txt','r')
str1=f1.read()
print(len(str1))
RECORD()

OR

Write a python function named CLRECORD to calculate


total number of lines in 8Myüle.txt9

def CLRECORD():
f1=open('Myüle.txt','r')
l1=f1.readlines()
print(len(l1))
CLRECORD()

#8.
Write a method count_words_e()in Python to read the
content of a textüle and count
the number of words ending with 'e' in the üle.
Example: If the üle content is as follows:
An apple a day keeps the doctor away.
We all pray for everyone9s safety.
A marked diûerence will come in our country.
The count_words_e() function should display the output as:
No.of such words: 4

def count_words_e():
f1=open("MyFile.txt")
data=f1.read()
w=data.split()
c=0
for i in w:
if i[-1]=='e':
c+=1
print(c)
count_words_e()

OR

Write a function reverseFile()in Python, which should read


the content of a text üle
<TESTFILE.TXT= and display all its line in the reverse
order.
Example: If the üle content is as follows:
It rained yesterday.
It might rain today.
I wish it rains tomorrow too.
I love Rain.
The RainCount()function should display the output as:
.yadretsey deniar tI
.yadot niar thgim tI
.oot worromot sniar ti hsiw I
.niaR evol I
def reverseFile():
f1=open("MyFile.txt")
data=f1.readlines()
for i in range(len(data)):
print(data[i][::-1])

#9 a) Write a program to count the words <to= and <the=


present in a text üle <python.txt=.

fname = "python.txt"
k= 0
f= open(fname, 'r')
words = f.read().split()
for a in words:
if (a.lower() == <to= or a.lower() == <the= ):
k=k+1
print("Number of words:", k)
f.close()

OR

b)
A text üle <PYTHON.TXT= contains alphanumeric text.
Write a program that reads this text üle and writes to
another üle <PYTHON1.TXT= entire üle except the numbers
or digits in the üle.

fh=open(<python.txt","r")
fw=open(<python1.txt","w")
rec=fh.read()
for a in rec:
if (a.isdigit() != True):
print(a,end=' ')
fw.write(a)
fh.close()
fw.close()
10 a)
Write a method/function COUNT_BLANK_SPACES() in
Python to read lines from a text üle
STORY.TXT, and display the count of blank spaces in the
text üle.

def COUNT_BLANK_SPACES():
f=open("STORY.txt",'r')
str=f.read()
x=str.split()
count=0
for i in x:
count+=1
print("Total no. blank spaces are ",count-1)
f.close()
COUNT_BLANK_SPACES():

OR

b)
Write a method/function DISPLAYWORDS() in python to
read lines from a text üle POEM.TXT, and
display those words, which are less than 4 characters.

def DISPLAYWORDS():
üle=open(>POEM.txt','r')
line = üle.read()
word = line.split()
for w in word:
if len(w)<4:
print( w)
üle.close()
DISPLAYWORDS()

11 a)
Write a function in python to count the number of lines in a
text üle 8Country.txt9 which are starting with an alphabet
8W9 or 8H9.
For example, If the üle 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 üll up with snow.
The output of the function should be:
W or w : 1
H or h : 2

def count_W_H():
f = open (<Country.txt=, <r=)
W,H = 0,0
r = f.read()
for x in r:
if x[0] == <W= or x[0] == <w=:
W=W+1
elif x[0] == <H= or x[0] == <h=:
H=H+1
f.close()
print (<W or w :=, W)
print (<H or h :=, H)

OR

Write a user deüned function to display the total number of


words
present in a text üle 'Quotes.txt'

def countwords():
s = open("Quotes.txt","r")
f = s.read()
z = f.split ()
count = 0
for I in z:
count = count + 1
print ("Total number of words:", count)

#12 a)
Write a function COUNTLINES( ) which reads a text üle
STORY.TXT and then count and
display the number of the lines which starts and ends with
same letter irrespective of its
case . For example if the content of the text üle STORY.TXT
is :
The person has a sent a lovely tweet
Boy standing at station is very disturbed
Even when there is no light we can see
How lovely is the situation
The expected output is :
The Number of lines starting with same letter is 2

Ans :
def COUNTLINES():
ün = open("Story.txt", 'r')
lines = ün.readlines()
count = 0
for line in lines:
if line[0].lower() == line[-1].lower():
count+=1
print("The Number of lines starting with same letter
is",count)
ün.close()

OR

Write a function VOWEL_WORDS which reads a text üle


TESTFILE.TXT and then count and
display the number of words starting with vowels 8a9 or 8u9
(including capital cases A and U
too)
For example is the text in the üle TESTFILE.txt is :
The train from Andaman has earned the name 8Floating
Train9. What is so unique about this
train to receive such a name?
The expected output is :
The Number of words starting with letter 8a9 is : 3
The Number of words starting with letter 8u9 is : 1

Ans :
def VOWEL_WORDS():
ün = open("TESTFILE.TXT" , 'r')
content = ün.read()
words = content.split()
acount,ucount = 0,0
for word in words:
if word[0] in "aA":
acount+=1
if word[0] in "uU":
ucount+=1
print("The Number of words starting with letter 8a9 is
:",acount)
print("The Number of words starting with letter 8u9 is
:",ucount)
VOWEL_WORDS()

#13 a)
Deüne a function SHOWWORD () in python to read lines
from a text üle STORY.TXT, and
display those words, whose length is less than 5.

def SHOWWORD () :
c=0
üle=open(8STORY.TXT,'r')
line = üle.read()
word = line.split()
for w in word:
if len(w)<5:
print( w)
üle.close()

OR

Write a user deüned function in python that displays the


number of lines starting with 'H' in the üle para.txt

def count H( ):
f = open (<para.txt= , <r= )
lines =0
L=f. readlines ()
for i in L:
if i [0]== 8H9:
lines +=1
print (<No. of lines are: < , lines)

#14.
W rite a method SHOWLINES() in Python to read lines
from text üle
8TESTFILE.TXT9 and display the lines which do not contain
'ke'.
Example: If the üle content is as follows:
An apple a day keeps the doctor away.
We all pray for everyone9s safety.
A marked diûerence will come in our country.
The SHOWLINES() function should display the output as:
We all pray for everyone9s safety.

def SHOWLINES():
f=open("testüle.txt")
for line in f:
if 'ke' not in line:
print(line.strip())
f.close()
OR

Write a function RainCount() in Python, which should read


the content of
a text üle <TESTFILE.TXT= and then count and display the
count of occurrence
of word RAIN (case-insensitive) in the üle.
Example: If the üle content is as follows:
It rained yesterday
It might rain today
I wish it rains tomorrow too
I love Rain
The RainCount() function should display the output as: Rain
-2

def RainCount():
f=open('rain.txt')
data=f.read()
data=data.upper()
data=data.split()
c=data.count('RAIN')
print('Rain -',c)

You might also like