Skip to content

Instantly share code, notes, and snippets.

@robsmith1776
Created June 26, 2015 13:48
Show Gist options
  • Save robsmith1776/efac554de78203ed92fc to your computer and use it in GitHub Desktop.
Save robsmith1776/efac554de78203ed92fc to your computer and use it in GitHub Desktop.
chunk
"""splits a large text file into smaller ones, based on line count
Original is left unmodified.
Resulting text files are stored in the same directory as the original file.
Useful for breaking up text-based logs or blocks of login credentials.
"""
import os
def split_file(filepath, lines_per_file=100):
"""splits file at `filepath` into sub-files of length `lines_per_file`
"""
lpf = lines_per_file
path, filename = os.path.split(filepath)
with open(filepath, 'r') as r:
name, ext = os.path.splitext(filename)
try:
w = open(os.path.join(path, '{}_{}{}'.format(name, 0, ext)), 'w')
for i, line in enumerate(r):
if not i % lpf:
#possible enhancement: don't check modulo lpf on each pass
#keep a counter variable, and reset on each checkpoint lpf.
w.close()
filename = os.path.join(path,
'{}_{}{}'.format(name, i, ext))
w = open(filename, 'w')
w.write(line)
finally:
w.close()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment