Q. Write a method in python to read lines from a text file MYNOTES.TXT, and display those lines, which are starting with an alphabet "K".


Answer :-

def display():
    file = open("MYNOTES.TXT", 'r')
    line = file.readline()
    while line:
        if line[0] == 'K' :
            print (line)
        line = file.readline()
    file.close()


Let file MYNOTES.TXT contains:-

" India is the fastest growing economy.
India is looking for more investments around the globe.
Keep the whole world is looking at India as a great market.
Most of the Indians can foresee the heights that India is
capable of reaching. "


Then output is :-

>>> display()
Keep the whole world is looking at India as a great market.

>>>

Post a Comment

You can help us by Clicking on ads. ^_^
Please do not send spam comment : )

Previous Post Next Post