Professional Documents
Culture Documents
Write a function in python to count the number Write a user defined function countwords() to
lines in a text file ‘Country.txt’ which is starting display the total number of words present in the
with an alphabet ‘W’ or ‘H’. file from a text file “Quotes.Txt”.
def count_W_H():
f = open (“Country.txt”, “r”) def countwords():
W,H = 0,0 s = open("Quotes.txt","r")
r = f.read() f = s.read()
for x in r: z = f.split ()
if x[0] == “W” or x[0] == “w”: count = 0
W=W+1 for i in z:
elif x[0] == “H” or x[0] == “h”: count = count + 1
H=H+1 print ("Total number of words:", count)
f.close()
print (“W or w :”, W)
print (“H or h :”, H)
Write a user defined function countwords() to Write a function COUNT_AND( ) in Python to read
display the total number of words present in the the text file “STORY.TXT” and count the number
file from a text file “Quotes.Txt”. of times “AND” occurs in the file. (include
def countwords(): AND/and/And in the counting)
s = open("Quotes.txt","r") def COUNT_AND( ):
f = s.read() count=0
z = f.split () file=open(‘STORY.TXT','r')
count = 0 line = file.read()
for i in z: word = line.split()
count = count + 1 for w in word:
print ("Total number of words:", count) if w ==’AND’:
count=count+1
print(count)
file.close()
Write a function DISPLAYWORDS( ) in python to Write a function that counts and display the
display the count of words starting with “t” or “T” number of 5 letter words in a text file “Sample.txt
in a text file ‘STORY.TXT’. def count_words( ):
def COUNT_AND( ): c=0
count=0 f = open("Sample.txt")
file=open(‘STORY.TXT','r') line = f.read()
line = file.read() word = line.split()
word = line.split() for w in word:
for w in word: if len(w) == 5:
if w[0] ==’t’ or w[0]==’T’: c += 1
count=count+1 print(c)
print(count)
file.close()
Write a function that counts and display the Write a function that counts and display the
number of 5 letter words in a text file “Sample.txt number of 5 letter words in a text file “Sample.txt
def count_words( ): def count_words( ):
c=0 c=0
f = open("Sample.txt") f = open("Sample.txt")
line = f.read() line = f.read()
word = line.split() word = line.split()
for w in word: for w in word:
if len(w) == 5: if len(w) == 5:
c += 1 c += 1
print(c) print(c)
Write a function that counts and display the Write a function to display those lines which start
number of 5 letter words in a text file “Sample.txt with the letter “G” from the text file
def count_words( ): “MyNotes.txt”
c=0 def count_lines( ):
f = open("Sample.txt") c=0
line = f.read() f = open("MyNotes.txt")
word = line.split() line = f.readlines()
for w in word: for w in line:
if len(w) == 5: if w[0] == 'G':
c += 1 print(w)
print(c) f.close()
f.close()
Write a function in python to read lines from file Write a function COUNT() in Python to read
“POEM.txt” and display all those words, which contents from file “REPEATED.TXT”, to count and
has two characters in it. display the occurrence of the word “Catholic” or
def TwoCharWord(): “mother”.
f = open('poem.txt') def COUNT():
count = 0 f = open('REPEATED.txt')
for line in f: count = 0
words = line.split() for line in f:
for w in words: words = line.split()
if len(w)==2: for w in words:
if w.lower()=='catholic' or w.lower()=='mother':
print(w,end=' ')
f.close() count+=1
print('Count of Catholic,mother is',count)
Write a function in python that displays the Write a function countmy() in Python to read file
number of lines starting with ‘H’ in the file Data.txt and count the number of times “my”
“para.txt”. occur in file.
def countH(): def countmy():
f=open("para.txt","r") f=open(“Data.txt”,”r”)
lines=0 count=0
l=f.readlines() x=f.read()
for i in l: word=x.split()
if i[0]='H': for i in word:
lines+=1 if i ==”my” :
print("NO of lines are:",lines) count=count+1
f.close() print(“my occurs “, count, “times”)
Write a Python program to find the number of Write a Python program to count the word “if “ in
lines in a text file ‘abc.txt’. a text file abc.txt’.
f=open("abc.txt","r") file=open("abc.txt","r")
d=f.readlines() c=0
count=len(d) line = file.read()
print(count) word = line.split()
f.close() for w in word:
if w=='if':
print( w)
c=c+1
print(c)
file.close()
Write a method in python to read lines from a Write a method/function ISTOUPCOUNT() in
text file DIARY.TXT and display those lines which python to read contents from a text file
start with the alphabets P. WRITER.TXT, to count and display the occurrence
def countp(): of the word ‘‘IS’’ or ‘‘TO’’ or ‘‘UP’’
f=open("diary.txt","r")
lines=0 def ISTOUPCOUNT():
l=f.readlines() c=0
for i in l: file=open('sample.txt','r')
if i[0]='P': line = file.read()
lines+=1 word = line.split()
print("No of lines are:",lines) cnt=0
for w in word:
if w=='TO' or w=='UP' or w=='IS':
cnt+=1
print(cnt)
file.close()
Write a code in Python that counts the number of Write a function VowelCount() in Python, which
“The” or “This” words present in a text file should read each character of a text file
“MY_TEXT_FILE.TXT”. MY_TEXT_FILE.TXT, should count and display the
c=0 occurrence of alphabets vowels.
f=open('MY_TEXT_FILE.TXT', 'r') :
d=f.read() def VowelCount():
w=d.split() count_a=count_e=count_i=count_o=count_u=0
for i in w: f= open('MY_TEXT_FILE.TXT', 'r')
if i.upper()== 'THE' or i.upper()== 'THIS' : d=f.read()
c+=1 for i in d:
print(c) if i.upper()=='A':
count_a+=1
elif letter.upper()=='E':
count_e+=1
elif letter.upper()=='I':
count_i+=1
elif letter.upper()=='O':
count_o+=1
elif letter.upper()=='U':
count_u+=1
print("A or a:", count_a)
print("E or e:", count_e)
print("I or i:", count_i)
print("O or o:", count_o)
print("U or u:", count_u)
Write a function filter(oldfile, newfile) that copies
all the lines of a text file “source.txt” onto
“target.txt” except those lines which starts with
“@” sign.