Python 3 reading txt file | SoloLearn: Learn to code for FREE!


Python 3 reading txt file

I got txt file called RR.txt that has On and Off words so i want my program to read txt file and if there is word On in first line it will print("On") Can someone please help me I have been trying for 1 hour and it didn't work. RR.txt: Mode: On Ww: Off

4/12/2019 1:38:09 PM

Damjan Boxer

1 Answer

New Answer


with open('rr.txt', 'rt') as f: for line in f: if line.strip() == 'On': print('on') else: print('off') break alternative to the third line: if 'On' in line.strip(): (if there's more text in the first line than just 'On')