Back

Explore Courses Blog Tutorials Interview Questions
0 votes
2 views
in Python by (45.3k points)

I need to search a pretty large text file for a particular string. Its a build log with about 5000 lines of text. What's 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.

1 Answer

0 votes
by (16.8k points)

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

Related questions

Browse Categories

...