Created
December 11, 2013 11:35
-
-
Save habibutsu/7908903 to your computer and use it in GitHub Desktop.
Split file by specific string
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
import mmap | |
import os | |
from sys import stdout | |
from sys import exit | |
# If length is 0, the maximum length of the map is the current size of the file | |
FIND_BUFFER_SIZE = 0 | |
READ_BUFFER_SIZE = 100 | |
SEARCH_STRING = "substring3.1" | |
f = open('example.txt') | |
mmf = mmap.mmap(f.fileno(), FIND_BUFFER_SIZE, access=mmap.ACCESS_READ) | |
index = mmf.find(SEARCH_STRING) | |
if index == -1: | |
print("string not found") | |
exit(-1) | |
mmf.seek(index, os.SEEK_SET) | |
length = mmf.size() | |
while True: | |
stdout.write(mmf.read(READ_BUFFER_SIZE)) | |
if length == mmf.tell(): | |
break |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment