Skip to content

Instantly share code, notes, and snippets.

@esafwan
Forked from mattiasostmar/chunker.py
Created November 28, 2015 14:45

Revisions

  1. @mattiasostmar mattiasostmar revised this gist Dec 10, 2013. 1 changed file with 3 additions and 3 deletions.
    6 changes: 3 additions & 3 deletions chunker.py
    Original file line number Diff line number Diff line change
    @@ -42,6 +42,6 @@ def joinFiles(fileName,noOfChunks,chunkSize):
    dataList.append(f.read())
    f.close()
    for data in dataList:
    f2 = open(fileName, 'wb')
    f2.write(data)
    f2.close()
    f2 = open(fileName, 'wb')
    f2.write(data)
    f2.close()
  2. @mattiasostmar mattiasostmar revised this gist Dec 10, 2013. 1 changed file with 1 addition and 2 deletions.
    3 changes: 1 addition & 2 deletions chunker.py
    Original file line number Diff line number Diff line change
    @@ -41,8 +41,7 @@ def joinFiles(fileName,noOfChunks,chunkSize):
    f = open(chunkName, 'rb')
    dataList.append(f.read())
    f.close()
    #Alteration: I open the files as f2 within the loop.
    for data in dataList:
    for data in dataList:
    f2 = open(fileName, 'wb')
    f2.write(data)
    f2.close()
  3. @mattiasostmar mattiasostmar created this gist Dec 10, 2013.
    48 changes: 48 additions & 0 deletions chunker.py
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,48 @@
    # 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()
    #Alteration: I open the files as f2 within the loop.
    for data in dataList:
    f2 = open(fileName, 'wb')
    f2.write(data)
    f2.close()