Write a function in python to count the number of lines from a text file "story.txt" which is not
starting with an alphabet "T".
Answers
Answered by
13
Answer:
def COUNTLINES():
file=open('story.txt','r')
lines = file.readlines()
count=0
for w in lines:
if w[0]!="T" or w[0]!="t":
count=count+1
print(“Total lines “,count)
file.close()
Answered by
0
Python Code
def line_count():
file = open("story.txt","r")
count=0
for line in file:
if line[0] not in 'T':
count+= 1
file.close()
print("No of lines not starting with 'T'=",count)
line_count()
- To read a specific line from a file, use the linecache. getline() method. the filename of line lineno in the specified file. This function will return an empty string rather than an error if the line is not present in the file.
- The easiest approach to determine the number of lines in a text file in Python is by using this method. All lines from a file are read by the readlines() method, which puts the lines in a list. Next, calculate the length of the list—which is simply the total number of lines in a file—using the len() function.
#SPJ3
Similar questions
Science,
3 months ago
Math,
3 months ago
Social Sciences,
3 months ago
Social Sciences,
6 months ago
Math,
6 months ago
Political Science,
10 months ago
Biology,
10 months ago