iman453 iman453 - 1 year ago 80
Python Question

Cheap way to search a large text file for a string

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 shouldn't cause any problems should it? I'll go ahead and read blocks of lines, and use the simple find.

Answer Source

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
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download