Skip to content

Instantly share code, notes, and snippets.

@SavvyGuard
Created August 19, 2013 18:03
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save SavvyGuard/6272171 to your computer and use it in GitHub Desktop.
Save SavvyGuard/6272171 to your computer and use it in GitHub Desktop.
split a file into multiple parts by bytes
def split_file(file, prefix, max_size, buffer=1024):
"""
filename: the input filename
max_size: maximum size of each created file in bytes
buffer: buffer size in bytes
Returns the number of parts created.
"""
with open(file, 'r+b') as src:
suffix = 0
while True:
with open(filename + '.%s' % suffix, 'w+b') as tgt:
written = 0
while written <= max_size:
data = src.read(buffer)
if data:
tgt.write(data)
written += buffer
else:
return suffix
suffix += 1
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment