Python 2.4.3
I need to read through some files (can be as large as 10GB). What I need it to do is go through the file until it matches a pattern. Then print that l
You're still matching against line, which doesn't change because you're still in the same iteration of the for loop.
I don't see any need to use regex here. Not that they're that bad, but if you're looking for such a specific pattern, it's just overkill to use regex. Try something like this:
def record(filename, pattern):
with open(filename) as file:
recording = False
for line in file:
if line.startswith(pattern):
yield line
recording = not recording
elif recording:
yield line
Calling record
with a filename and your pattern gives you a generator object yielding line after line. This is better when dealing with large files, so you don't have to slurp them in at once.
Printing your lines could then work like this - assuming your file is named example.txt
:
for rec in record(filename, '---- Alpha ---- Zeta'):
print rec
To be precise: The record
generator yields the lines including the line breaks, so you might want to join
them back together without any additional line breaks:
print "".join(list(record(filename, '---- Alpha ---- Zeta')))