Created
October 15, 2017 13:34
-
-
Save tianchaijz/9351d5483eba4204f9209bfa3eb7b5bd to your computer and use it in GitHub Desktop.
Range file
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
| #!/usr/bin/env python | |
| # -*- coding: utf-8 -*- | |
| import sys | |
| def gen_byte(path): | |
| with open(path) as fd: | |
| while True: | |
| buf = fd.read(4096) | |
| if not buf: | |
| return | |
| for b in buf: | |
| yield b | |
| def range_filter(path, start, end=-1): | |
| def write(s): | |
| sys.stdout.write(s) | |
| buf = [] | |
| n = 0 | |
| end = end if end >= 0 else sys.maxint | |
| for b in gen_byte(path): | |
| if n >= start: | |
| buf.append(b) | |
| n += 1 | |
| if n > end: | |
| break | |
| if len(buf) > 4096: | |
| write(''.join(buf)) | |
| buf = [] | |
| if buf: | |
| write(''.join(buf)) | |
| path = sys.argv[1] | |
| r = sys.argv[2].split('-') | |
| if len(r) == 2: | |
| start, end = int(r[0]), int(r[1]) | |
| else: | |
| start, end = int(r[0]), -1 | |
| range_filter(path, start, end) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment