Skip to content

Instantly share code, notes, and snippets.

@costrouc
Created July 9, 2015 13:54
Show Gist options
  • Select an option

  • Save costrouc/5fe7338c96d534b0b8ac to your computer and use it in GitHub Desktop.

Select an option

Save costrouc/5fe7338c96d534b0b8ac to your computer and use it in GitHub Desktop.
Search large file with buffer
def fnd(fname, s, start=0):
with open(fname, 'rb') as f:
fsize = os.path.getsize(fname)
bsize = 4096
buffer = None
if start > 0:
f.seek(start)
overlap = len(s) - 1
while True:
if (f.tell() >= overlap and f.tell() < fsize):
f.seek(f.tell() - overlap)
buffer = f.read(bsize)
if buffer:
pos = buffer.find(s)
if pos >= 0:
return f.tell() - (len(buffer) - pos)
else:
return -1
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment