เพื่อแยกไฟล์ binary ใหญ่เป็นชิ้นเล็กๆ ให้อ่านตามขนาด chunk ที่ต้องการ เขียนลงไฟล์ใหม่ ทำซ้ำจนจบไฟล์ต้นฉบับ
ตัวอย่าง
แยก my_song.mp3 เป็นไฟล์ละ 500 bytes:
CHUNK_SIZE = 500
file_number = 1
with open('my_song.mp3', 'rb') as f:
chunk = f.read(CHUNK_SIZE)
while chunk:
with open('my_song_part_' + str(file_number), 'wb') as chunk_file:
chunk_file.write(chunk)
file_number += 1
chunk = f.read(CHUNK_SIZE)
หมายเหตุ: ต้องเปิดไฟล์ในโหมด binary (rb/wb) สำหรับไฟล์ binary
ในไดเรกทอรีปัจจุบันจะพบไฟล์ชิ้นต่างๆ นำหน้าด้วย my_song_part_