For Video Explanation of This Topic, Please Click On The Following Link
For Video Explanation of This Topic, Please Click On The Following Link
http://www.youtube.com/swatichawlaofficial
def BeginYou():
f=open("Alpha.txt","r")
lines=f.readlines()
for line in lines:
words=line.split()
if words[0]=='You':
print(line,end='')
f.close()
#BeginYou()
'''
Write a function, vowelCount() in Python that counts and
displays the number of vowels in the text file named
Alpha.txt.
'''
def vowelCount():
f=open("Alpha.txt","r")
count=0
data=f.read()
for i in data:
if i in 'AEIOUaeiou': #if i.lower() in 'aeiou'
count+=1
print("Number of vowels=",count)
f.close()
#vowelCount()
'''
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 names of 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
'''
def DisplayBook():
f=open("Bookname.txt","r")
lines=f.readlines()
for line in lines:
if 'Y' in line or 'y' in line:
print(line,end='')
f.close()
#DisplayBook()
'''
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)
'''
def RevString():
f=open("Input.txt")
data=f.read()
words=data.split()
for word in words:
if word[0]=='O':
print(word[::-1],end=' ')
else:
print(word,end=' ')
f.close()
#RevString()
'''
Write the definition of a Python function named LongLines( )
which reads the contents of a text file named 'LINES.TXT' and
displays 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 wood.
'''
Write a function count_Dwords() in Python to count the words
ending with a digit 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
'''
def count_Dwords():
f=open("Details.txt","r")
count=0
data=f.read()
words=data.split()
for word in words:
if word[-1].isdigit():
count+=1
print("Number of words ending with a digit are",count)
f.close()
#count_Dwords()
'''
Write the definition of a function ChangeGender() in Python ,
which reads the contents of a text 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.
The function should read the file content and display the
output as
follows:
#Method 2
def ChangeGender():
f=open("BIOPIC.txt","r")
lines=f.readlines()
for line in lines:
words=line.split()
for word in words:
if word=='he':
print('she',end=' ')
else:
print(word,end=' ')
print()
f.close()
#ChangeGender()
'''
Write the definition of a function Count_Line() 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.
The function should read the file content and display the
output
as follows :
Total number of lines : 3
'''
def Count_Line():
f=open("SHIVAJI.txt")
lines=f.readlines()
length=len(lines)
print("Total number of lines :",length)
f.close()
#Count_Line()
'''
Write a function Show_words() in python to read the content of
a text file 'NOTES.TXT' and display the entire content in
capital letters.
Example, if the file contains:
"This is a test file"
Then the function should display the output as:
THIS IS A TEST FILE
'''
def Show_words():
f=open("NOTES.txt")
data=f.read()
print(data.upper())
f.close()
#Show_words()
'''
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 have 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."
Then the function should display the output as:
This is a sample file.
The file contains many sentences.
'''
def Show_words():
f=open("NOTES.txt","r")
lines=f.readlines()
for line in lines:
words=line.split()
if len(words)==5:
print(line,end='')
f.close()
#Show_words()
'''
Write a method COUNTLINES() in Python to read lines from text
file ‘TESTFILE.TXT’ count 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.
def COUNTLINES():
f=open("TESTFILE.txt","r")
count=0
lines=f.readlines()
for line in lines:
if line[0] not in 'AEIOUaeiou':
count+=1
print("The number of lines not starting with any vowel
=",count)
f.close()
#COUNTLINES()
'''
Write a function ETCount() in Python, which should read each
character of a text file “TESTFILE2.TXT” 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
def ETCount():
EC=TC=0
f=open("TESTFILE2.txt","r")
data=f.read()
for i in data:
if i =='E' or i=='e': # if i in 'Ee':
EC+=1
elif i=='T' or i=='t': # if i in 'Tt'
TC+=1
print("The number of E or e:",EC)
print("The number of T or t:",TC)
f.close()
#ETCount()
'''
Write a function in Python that counts the number of “Me” or
“My” words present in a text file “STORY.TXT”.
If the “STORY.TXT” contents are as follows:
def Count_Me_My():
f=open("STORY.txt","r")
count=0
data=f.read()
words=data.split()
for word in words:
if word=="Me" or word=='My':
count+=1
print("Count of Me/My in file: ",count)
#Count_Me_My()
'''
Write a function in python to count the number of lines in a
text file ‘STORY.TXT’ which is starting with an alphabet ‘A’
.
'''
def Count_Line_Start_A():
f=open("STORY.txt","r")
lines=f.readlines()
for line in lines:
if line.startswith('A'): # if line[0]=='A':
print(line,end='')
f.close()
#Count_Line_Start_A()
'''
Write a method/function DISPLAYWORDS() in python to read lines
from a text file STORY.TXT, and display those words, which are
less than 4 characters.
'''
def DISPLAYWORDS():
f=open("STORY.txt","r")
data=f.read()
words=data.split()
for word in words:
if len(word)<4:
print(word)
f.close()
DISPLAYWORDS()