
- Python Basic Tutorial
- Python - Home
- Python - Overview
- Python - Environment Setup
- Python - Basic Syntax
- Python - Comments
- Python - Variables
- Python - Data Types
- Python - Operators
- Python - Decision Making
- Python - Loops
- Python - Numbers
- Python - Strings
- Python - Lists
- Python - Tuples
- Python - Dictionary
- Python - Date & Time
- Python - Functions
- Python - Modules
- Python - Files I/O
- Python - Exceptions
How to spilt a binary file into multiple files using Python?
To split a big binary file in multiple files, you should first read the file by the size of chunk you want to create, then write that chunk to a file, read the next chunk and repeat until you reach the end of original file.
Example
For example, you have a file called my_song.mp3 and want to split it in files of size 500 bytes each.
CHUNK_SIZE = 500 file_number = 1 with open('my_song.mp3') as f: chunk = f.read(CHUNK_SIZE) while chunk: with open('my_song_part_' + str(file_number)) as chunk_file: chunk_file.write(chunk) file_number += 1 chunk = f.read(CHUNK_SIZE)
In your current directory, now you'll find chunks of your original file scattered across multiple files with prefix as: my_song_part_
- Related Articles
- How to merge multiple files into a new file using Python?
- How to concatenate two files into a new file using Python?
- How to open multiple files using a File Chooser in JavaFX?
- How to Append Contents of Multiple Files Into One File on Linux?
- Python - Write multiple files data to master file
- How to rename multiple files recursively using Python?
- How are files added to a tar file using Python?
- How are files added to a zip file using Python?
- How we can split Python class into multiple files?
- How to write binary data to a file using Python?
- Rename multiple files using Python
- How are files extracted from a tar file using Python?
- How to save multiple plots into a single HTML file in Python Plotly?
- How to Merge multiple CSV Files into a single Pandas dataframe ?
- Python Pandas- Create multiple CSV files from existing CSV file

Advertisements