Created
August 19, 2013 18:03
-
-
Save SavvyGuard/6272171 to your computer and use it in GitHub Desktop.
split a file into multiple parts by bytes
This file contains 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
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