簡體   English   中英

如何使用python將文件分為幾個文件

[英]How to divide file into several files using python

我有一個視頻文件,我需要其分成幾個大小為256KB的較小文件,並將所有文件名保存在文本文件中,然后我需要讀取所有較小的文件並將其合並為原始文件。

這有可能在python中做到嗎?

分裂的第一步:

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()

這可能會讓您一臂之力; 根據需要進行調整。

合並:

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()

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM