Wednesday, 29 January 2020

python read write large file by chunks, test video, pdf files

b.mp4 assembled from chunks

b.pdf assembled from chunks


import pickle
import glob
import os.path

def read_in_chunks(file_object, chunk_size=1024*1024):

    while True:
        data = file_object.read(chunk_size)
        if not data:
            break
        yield data


def store_chunks():
    with open('a.mp4', 'rb') as file:

        for i, piece in enumerate(read_in_chunks(file)):

            chunkName = str(i) + '.pickle'
            with open(os.path.join('pickled_chunks',  chunkName), 'wb') as chunk:
                pickle.dump(piece, chunk)


#store_chunks()

def assemble_chunks():
    chunks = glob.glob(os.path.join('pickled_chunks', '*.pickle'))

    with open('b.mp4', 'wb') as file:
        for i in range(len(chunks)):
            chunkName = str(i) + '.pickle'

            with open(os.path.join('pickled_chunks',  chunkName), 'rb') as pickled_chunk:
                file_chunk = pickle.load(pickled_chunk)
                file.write(file_chunk)


assemble_chunks()

reference:
http://chuanshuoge2.blogspot.com/2020/01/python-read-write-large-file-by-chunks.html

No comments:

Post a Comment