TEXT FILES
1. Write a function in python to read a text file Alpha.txt and displays those
lines which begin with the word 'You'
Alpha.txt
You and Me
Me and You
You and You
Me and Me
PROGRAM
def countyou() :
f = open("alpha.txt",'r')
l = f.readlines()
for i in l:
w = i.split()
if w[0] == 'You':
print(i, end='')
f.close()
countyou()
OUTPUT
You and Me
You and You
2. Write a function VowelCount() in Python that counts and displays the
number of vowels in the text file named Poem.txt
POEM.TXT
You and Me
Me and You
You and You
Me and Me
PROGRAM
def vowelcount():
f = open("poem.txt",'r')
s = f.read()
v=0
for i in s:
if i in 'AEIOUaeiou':
v+=1
print("Total number of Vowels is :",v)
f.close()
vowelcount()
OUTPUT
Total number of Vowels is : 16
3.Write a function in Python that displays the book names having ‘Y' or ‘y’ in
their name from a text file "Bookname.txt".
Example
if the file "Bookname.txt" contains. the name of the following books
One Hundred Years of Solitude
The Diary of a Young girl
On the Road
After execution the output will be
One Hundred Years of Solitude
The Diary of a Young Girl
Bookname.txt
One Hundred Years of Solitude
The Diary of a Young girl
On the Road
PROGRAM
#Display bookname Having Y
def BookName():
f = open("Bookname.txt")
a = f.readlines()
for i in a:
if 'Y' in i or 'y' in i:
print(i,end = '')
f.close()
BookName()
OUTPUT
One Hundred Years of Solitude
The Diary of a Young girl
4. Write a function RevString() to read a textfile "Input.txt” and Prints the
words starting with ‘O' in reverse order. The rest of the content is displayed
normally.
Example
If content in the text file is
UBUNTU IS AN OPEN SOURCE OPERATING SYSTEM
output will be
UBUNTU IS AN NEPO SOURCE GNITAREPO SYSTEM
(words 'OPEN' and 'OPERATING' are displayed in reverse order)
Input.txt
UBUNTU IS AN OPEN SOURCE OPERATING SYSTEM
PROGRAM
def RevString():
f = open("Input.txt")
a = f.read()
w = a.split()
for i in w:
if i[0] == 'O':
print(i[::-1],end = ' ')
else:
print(i,end = ' ')
f.close()
RevString()
OUTPUT
UBUNTU IS AN NEPO SOURCE GNITAREPO SYSTEM
5. Write the definition of a python function named LongLines() which reads the
contents of a text file named ‘Lines.txt’ and display those lines from the file
which have at least 10 words in it. For example if the content of ‘Lines.Txt’ is
as follows
Once upon a time, there was a woodcutter.
He lived in a little house in a beautiful, green wood.
One day, he was merrily chopping Some wood.
He saw a little girl skipping through the woods, whistling happily.
The girl was followed by a big gray wolf.
Then the function should display output as
He lived in a little house in a beautiful, green woods.
He saw a little girl skipping through the woods, whistling happily.
PROGRAM
def Longlines():
f = open("Lines.txt")
a = f.readlines()
for i in a :
w = i.split()
if len(w)>=10:
print(i,end='')
f.close()
Longlines()
OUTPUT
He lived in a little house in a beautiful, green wood.
He saw a little girl skipping through the woods, whistling happily.
6. Write a function Count_Dwords() in Python to count the words ending with
in a text file 'Details.txt’.
Example
If the file content is as follows.
On seat2 VIP1 will sit and
On Seat1 VVIP2 will be sitting
Output will be
Number of words ending with a digit are 4
PROGRAM
def Count_Dwords():
f = open("Details.txt")
c=0
a = f.read()
w = a.split()
for i in w:
if i[-1].isdigit() == True:
c+=1
print("Number of words ending with a digit are:",c)
f.close()
Count_Dwords()
OUTPUT
Number of words ending with a digit are: 4
7. Write the definition of function ChangeGender() in python, which reads the
contents of a teat file “BIOPIC.TXT” and displays the content of the file with
every occurrence of the word "he” replaced by “She”. For example, If the
content of the file "BIOPIC. TXT" is as follows
Last time he went to Agra,
there was too much Crowd, which he did not like.
So this time he decided to visit some hill Station
OutPut
Last Lime she went to Agra,
there was too much crowd which she did not like.
So this time she decided to visit some hill Station.
PROGRAM
def ChangeGender():
f = open("BIOPIC.txt")
a = f.read()
text = a.replace(' he ',' she ')
print(text)
f.close()
ChangeGender()
OUTPUT
Last time she went to Agra,
there was too much Crowd, which she did not like.
So this time she decided to visit some hill Station
8. Write the definition of a function countLine() in Python which should read
each line of a text file "SHIVAJI.TXT" and Count total number of lines present
in text file.
For example, if the content of the file "SHIVAJI .TXT is as follows
Shivaji was born in the family of Bhonsle.
He was devoted to his mother Jijabai.
India at that time was under Muslim rule.
output
Total number of Lines: 3
PROGRAM
def Count_lines():
f = open("SHIVAJI.txt")
a = f.readlines()
l = len(a)
print("Total Number of Lines :",l)
f.close()
Count_lines()
OUTPUT
Total Number of Lines : 3
9. Write a function show_words in python to read the content of a text file
“Notes.txt" and displays the entire Content in capital Letters.
Example
If the file contains
This is a test file
output
THIS IS A TEXT FILE
PROGRAM
def Show_words():
f = open("Notes.txt")
a = f.read()
print(a.upper())
f.close()
Show_words()
OUTPUT
THIS IS A TEST FILE
10. Write a function show_words() in Python to read the content of a text file
“Notes.txt” and display only such lines of the file which hove exactly 5 words
in them.
Example if the file contains
This is a Sample file
The file contains many sentences
But need only sentences which have only 5 words
Output
This is a Sample file.
The file contains many sentences.
PROGRAM
def Show_words():
f = open("Notes.txt")
a = f.readlines()
for i in a :
w = i.split()
if len(w) == 5:
print(i,end=' ')
f.close()
Show_words()
OUTPUT
This is a Sample file
The file contains many sentences