Python: read all text file lines in loop

前端 未结 4 889
予麋鹿
予麋鹿 2020-11-28 08:56

I want to read huge text file line by line (and stop if a line with \"str\" found). How to check, if file-end is reached?

fn = \'t.log\'
f = open(fn, \'r\')
         


        
4条回答
  •  旧时难觅i
    2020-11-28 09:45

    You can stop the 2-line separation in the output by using

        with open('t.ini') as f:
           for line in f:
               print line.strip()
               if 'str' in line:
                  break
    

提交回复
热议问题