Skip to content

Instantly share code, notes, and snippets.

@mattiasostmar
Last active September 16, 2018 13:53
Show Gist options
  • Star 4 You must be signed in to star a gist
  • Fork 2 You must be signed in to fork a gist
  • Save mattiasostmar/7883550 to your computer and use it in GitHub Desktop.
Save mattiasostmar/7883550 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...)
# 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()
@esafwan
Copy link

esafwan commented Nov 28, 2015

Can we use this to send large video files over UDP protocol?

@AntonioSlzr
Copy link

AntonioSlzr commented Dec 20, 2017

Damn, I really needed it... thanks

@csmunuku
Copy link

Thanks for the code - I made few more changes.. check it out here
https://github.com/csmunuku/file_splitter_joiner

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment