-
-
Save esafwan/92513303b18b51a66c33 to your computer and use it in GitHub Desktop.
A slight moderation to get the code working on http://bdurblg.blogspot.se/2011/06/python-split-any-file-binary-to.html (darn, need to get those indentations right...)
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
# define the function to split the file into smaller chunks | |
def splitFile(inputFile,chunkSize): | |
#read the contents of the file | |
f = open(inputFile, 'rb') | |
data = f.read() | |
f.close() | |
# get the length of data, ie size of the input file in bytes | |
bytes = len(data) | |
#calculate the number of chunks to be created | |
noOfChunks= bytes/chunkSize | |
if(bytes%chunkSize): | |
noOfChunks+=1 | |
#create a info.txt file for writing metadata | |
f = open('info.txt', 'w') | |
f.write(inputFile+','+'chunk,'+str(noOfChunks)+','+str(chunkSize)) | |
f.close() | |
chunkNames = [] | |
for i in range(0, bytes+1, chunkSize): | |
fn1 = "chunk%s" % i | |
chunkNames.append(fn1) | |
f = open(fn1, 'wb') | |
f.write(data[i:i+ chunkSize]) | |
f.close() | |
#define the function to join the chunks of files into a single file | |
def joinFiles(fileName,noOfChunks,chunkSize): | |
dataList = [] | |
for i in range(0,noOfChunks,1): | |
chunkNum=i * chunkSize | |
chunkName = fileName+'%s'%chunkNum | |
f = open(chunkName, 'rb') | |
dataList.append(f.read()) | |
f.close() | |
for data in dataList: | |
f2 = open(fileName, 'wb') | |
f2.write(data) | |
f2.close() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment