Text File Question and Answers
Text File Question and Answers
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_a(n):
f=open("story.txt","r")
a=f.readlines()
for x in a :
if x[0]=="A" :
print(x)
n=n+1
print("Count: ",n)
c=0
count_line_a(c)
def displaywords() :
f=open("story.txt","r")
a=f.read().split()
for x in a:
if len(x)<4 :
print(x)
displaywords()
Question
Write a method COUNTLINES() in Python to read lines from text file
TESTFILE.TXT‟ and display the lines which are not starting with any vowel
def countlines():
c=0
a=["a","e","i","o","u"]
f=open("story.txt","r")
tl=f.readlines()
for l in tl:
if l[0].lower() not in a:
print(l)
c=c+1
print("Count of lines which doesnot start with vowels: ",c)
countlines()
Write a function ETCount() in Python, which should read each character of a text
file “TESTFILE.TXT” and then count and display the count of occurrence of
alphabets E and T individually (including small cases e and t too).
def ETcount():
f=open("story.txt","r")
a=list(f.read())
ec=0
et=0
for x in a:
if x.lower() == "e":
ec=ec+1
if x.lower() =="t" :
et=et+1
print("Count of E or e: ",ec)
print("count of letter t or T: ",et)
ETcount()
write a function in python to read a text file,Alpha.txt and display those lines
which begin with t he word you
def disp_you():
f=open("story.txt","r")
a=f.readlines()
for x in a:
y=x.split()
#print(y)
if y[0]=="You" :
print(x)
disp_you()
Write a function ,vowelcount() in python that counts and displays the number of
vowels in the text file named poem.txt
def Vowelcount():
a=["a","e","i","o","u"]
f=open("story.txt","r")
b=list(f.read())
c=0
for x in b:
if x in a:
c=c+1
print("Count of vowel: ",c)
Vowelcount()
Write a Python functiongovWeb() that displays all the words containing gov.in
from a text file “URLs.txt”
def functiongovWeb():
f=open("story.txt","r")
a=(f.read()).split()
for x in a:
if "gov.in" in x:
print(x)
functiongovWeb()
write a Python function atleast5() that finds and displays all the words having at
least 5 characters from a text file “Story.txt”.
def atleast5() :
f=open("story.txt","r")
s=(f.read()).split()
for x in s:
if len(x)>=5 :
print(x)
atleast5()
Write a Python function count_vowels() that reads text from a file named
"input.txt" and counts the number of vowels (a, e, i, o, u) in the file. The function
should return the vowel count.
def count_vowels():
f=open("story.txt","r")
a=["a","e","i","o","u"]
b=list(f.read())
c=0
for x in b:
if x in a:
c=c+1
print("count of vowels: ",c)
count_vowels()
Write a Python function longest_word() that reads text from a file "words.txt"
and returns the longest word in the file.
def longest_word():
f=open("story.txt","r")
a=(f.read()).split()
l=0
lw=0
for x in a:
if len(x)>l:
l=len(x)
lw=x
print("the longest word is: ",lw,"with the length of :",l)
longest_word()
Write a Python function that extracts and displays all the words containing a
hyphen ("-") from a text file "HyphenatedWords.txt", which has a three letter word
before hypen and four letter word after hypen.
For example : "for-them" is such a word.
def contain_hyphen():
f=open("story.txt","r")
a=(f.read()).split()
for x in a:
y=x.split("-")
if len(y)==2 :
if len(y[0])==3 and len(y[1])==4 :
print(x)
contain_hyphen()
Write a Python function that count the lines start with the word “the” in a file
"xyz.txt" and display it at the end.
def line_the():
f=open("story.txt","r")
a=f.readlines()
c=0
for x in a:
y=x.split()
if y[0]=="the" :
print(x)
c=c+1
print("count of line: ",c)
line_the()
count of words beginning with capital letter Write a function that counts no of
words beginning with a capital letter from the text file RatanJi.txt
Write a function that displays the line number along with no of words in it from
the file Quotes.txt
def print_len():
f=open("story.txt","r")
a=f.readlines()
print("line Number \t\t No of words")
for x in range(len(a)):
y=a[x].split()
print("Line ",x+1,": \t\t ",len(y))
print_len()
Write a method SHOWLINES() in Python to read lines from text file 'EXAMCS.txt'
and display the lines which do not contain 'ke'.
def showlines():
f=open("exams.txt","r")
a=f.readlines()
for x in a:
if "ke" not in x:
print(x)
showlines()
Write a function RainCount() in Python, which should read the content of a text
file "RAIN.txt" and then count and display the count of occurrence of word rain
(case-insensitive) in the file.
def Raincount():
f=open("rain.txt","r")
a=(f.read()).split()
c=0
for x in a:
if x.lower()=="rain" :
c=c+1
print("count: ",c)
Raincount()