Q:

How to divide file into several files using python

I have a video file and i need to divide it into several smaller files of size 256KB and save all files names in a text file then i need to read all the small files and merges them into the original file.
is this possible to do it in python and how ?

A:

First stab at splitting:
input_file = open(input_filename, 'rb')
blocksize = 4096
chunksize = 1024 * 256
buf = None
chunk_num = 0
current_read = 0
output_filename = 'output-chunk-{:04d}'.format(chunk_num)
output_file = open(output_filename, 'wb')
while buf is None or len(buf) > 0:
    buf = input_file.read(blocksize)
    current_read += len(buf)
    output_file.write(buf)
    if chunksize <= current_read:
        output_file.close()
        current_read = 0
        chunk_num += 1
        output_filename = 'output-chunk-{:04d}'.format(chunk_num)
        output_file = open(output_filename, 'wb')
output_file.close()
input_file.close()

This might get you partway there; adapt as needed.
Merging:
blocksize = 4096
chunk_num = 0
input_filename = 'output-chunk-{:04d}'.format(chunk_num)
output_filename = 'reconstructed.bin'
output_file = open(output_filename, 'wb')
while True:
    try:
        input_file = open(input_filename, 'rb')
    except IOError:
        break
    buf = None
    while buf is None or len(buf) > 0:
        buf = input_file.read(blocksize)
        output_file.write(buf)
    input_file.close()
    chunk_num += 1
    input_filename = 'output-chunk-{:04d}'.format(chunk_num)
output_file.close()

