Created
July 9, 2015 13:54
-
-
Save costrouc/5fe7338c96d534b0b8ac to your computer and use it in GitHub Desktop.
Search large file with buffer
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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