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.

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

 

Answer:

def COUNTLINES ():

    count= 0

    f=open( 'TESTFILE.txt' , 'r' )

    lines=f.readlines()

    for line in lines:

        if (line[ 0 ]).lower() not in 'aeiou' :

            count=count+ 1

    print( "Number of lines not starting with vowels: " ,count)

    f.close()

Question 27(Choice 1) -Teachoo.png

Slide1.JPG

 

Remove Ads
Teachoo · Class 12 Explore Class 12
Davneet Singh's photo - Co-founder, Teachoo

Made by

Davneet Singh

Davneet Singh is an IIT Kanpur graduate and has been teaching for 16+ years. At Teachoo, he breaks down Maths, Science and Computer Science into simple steps so students understand concepts deeply and score with confidence.

Many students prefer Teachoo Black for a smooth, ad-free learning experience.