def vowelCount(): f = open("Poem.txt", "r") count = 0 s = f.read( ) for i in s: if i in 'AEIOUaeiou': count = count + 1 print("Number of vowels = ", count) f.close() vowelCount()
def youLines(): f = open("Alpha.txt", "r") count = 0 L = f.readlines() #[ "line1", "line2", .......] for line in L: words = line.split() #["word1", "word2", .......] if words[0] in ["you", "You", "YOU"]: print(line) f.close() youLines()
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(): f = open("Testfile.txt", "r") count_E = 0 count_T = 0 s = f.read() for i in s: if i == 'E' or i == 'e': count_E +=1 elif i == 'T' or i == 't': count_T +=1 print("E or e : ", count_E) print("T or t : ", count_T) f.close() ETCount()
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():
f = open("TESTFILE.txt", "r")
count = 0
L = f.readlines() #['line1', 'line2', ......]
for line in L:
if line[0] not in "AEIOUaeiou":
count += 1
print("Number of lines not starting with any vowel - ", count)
f.close()
COUNTLINES()
Example : If the “STORY.TXT” 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():
f = open("STORY.txt", "r")
count = 0
s=f.read()
wordlist = s.split() #[ 'word1', 'word2', ......]
for word in wordlist:
if word in [ 'Me', 'me', 'ME', 'mE', 'My', 'my', 'MY', 'mY' ]:
count = count + 1
print("Count of Me/My in file : ", count)
f.close()
countMeMy()
def DISPLAYWORDS():
f = open("Story.txt", "r")
s=f.read()
wordlist = s.split() #['word1', 'word2', ....]
for word in wordlist:
if len(word) < 4:
print(word)
f.close()
DISPLAYWORDS()
def A_lines():
f = open("story.txt", "r")
count = 0
lineslist = f.readlines() #['line1', 'line2', ....]
for line in lineslist:
if line[0] == 'A' or line[0] == 'a' :
count = count + 1
print("No. of lines starting with A = ", count)
f.close()
A_lines()