Cheap way to search a large text file for a string

后端 未结 9 1081
隐瞒了意图╮
隐瞒了意图╮ 2020-11-27 04:15

I need to search a pretty large text file for a particular string. Its a build log with about 5000 lines of text. Whats the best way to go about doing that? Using regex sho

9条回答
  •  轻奢々
    轻奢々 (楼主)
    2020-11-27 04:49

    If it is "pretty large" file, then access the lines sequentially and don't read the whole file into memory:

    with open('largeFile', 'r') as inF:
        for line in inF:
            if 'myString' in line:
                # do_something
    

提交回复
热议问题