Skip to content

Instantly share code, notes, and snippets.

@habibutsu
Created December 11, 2013 11:35
Show Gist options
  • Save habibutsu/7908903 to your computer and use it in GitHub Desktop.
Save habibutsu/7908903 to your computer and use it in GitHub Desktop.
Split file by specific string
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