How to divide file into several files using python
<p>I have a video file and i need to <strong>divide</strong> it into several smaller files of size <strong>256KB</strong> and save all files names in a text file then i need to read all the small files and <strong>merges</strong> them into the original file.</p>

<p>is this possible to do it in <strong>python</strong> and how ?</p>

<p>First stab at splitting:</p>

<pre><code>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) &gt; 0:
    buf = input_file.read(blocksize)
    current_read += len(buf)
    output_file.write(buf)
    if chunksize &lt;= 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()
</code></pre>

<p>This might get you partway there; adapt as needed.</p>

<p>Merging:</p>

<pre><code>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) &gt; 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()
</code></pre>

python python-2.7 python-3.x