Home > Enterprise >  How to selectively open lines in a txt file
How to selectively open lines in a txt file

Time:06-16

I have a txt file called song.txt and want to print out all the lines in the file which contain a certain word. I have tried using an if statement and am getting no output. Could anyone point me in the right direction?

**with open('song.txt', 'r') as s:
      if 'still' in s.readlines():
            print(s.readlines())**

FYI: I am an absolute amateur .

Thanks

CodePudding user response:

with open('song.txt', 'r') as s:
    for line in s.readlines():
        if 'still' in line:
            print(line)
    

CodePudding user response:

with open('song.txt', 'r') as s: 
    for line in s: # saves memory by not loading entire file.
        if 'still' in line:
            print(line)
  • Related