Computer Science

Write a function in Python to read a text file, Alpha.txt and displays those lines which begin with the word ‘You’.

Python File Handling

1 Like

Answer

The Alpha.txt file includes following data :

You To be or not to be, that is the question.
You The quick brown fox jumps over the lazy dog.
To infinity and beyond!
def test():
    fobj1 = open("Alpha.txt", "r")
    data = fobj1.readlines()
    for line in data:
        L = line.split()
        if L[0] == "You":
            print(line)
    fobj1.close()
test()
Output
You To be or not to be, that is the question.

You The quick brown fox jumps over the lazy dog.

Answered By

1 Like


Related Questions