File Splitter Program in Python
A Python file splitter is a coding program that converts a big file into smaller chunks or "chunks," depending on its size. This can be helpful when splitting large files for more straightforward storage, transmission, or processing. The split files can then be reunited if necessary. In Python, this is often accomplished by reading the file in chunks and writing every chunk to a new file with an incrementing name.
Full Code with Explanation:
Python Code
import os
def split_file(file_path, chunk_size):
"""
Split a file into smaller parts based on the specified chunk size.
Args:
file_path (str): Path to the input file.
chunk_size (int): Size of each output file in bytes.
"""
# Check if the file exists
if not os.path.isfile(file_path):
print(f"Error: File '{file_path}' does not exist.")
return
# Validate chunk size
if chunk_size <= 0:
print("Error: Chunk size must be a positive number.")
return
# Get the file size for progress tracking
file_size = os.path.getsize(file_path)
base_name = os.path.splitext(file_path)[0] # Extract base name without extension
# Open the input file in binary read mode
with open(file_path, 'rb') as f:
part_num = 1 # Part counter
bytes_processed = 0 # Track progress
# Read and split the file into chunks
while True:
chunk = f.read(chunk_size) # Read a chunk
if not chunk: # If no more data, exit loop
break
# Create output file name
output_file = f"{base_name}_part{part_num}"
# Write the chunk to a new file
with open(output_file, 'wb') as out_f:
out_f.write(chunk)
# Update progress
bytes_processed += len(chunk)
print(f"Created: {output_file} ({bytes_processed}/{file_size} bytes processed)")
part_num += 1
print("File splitting completed successfully!")
def main():
"""Main function to handle user input and run the splitter."""
# Get file path from user
file_path = input("Enter the path to the file you want to split: ")
# Get chunk size from user with error handling
try:
chunk_size = int(input("Enter the chunk size (in bytes): "))
split_file(file_path, chunk_size)
except ValueError:
print("Error: Please enter a valid number for chunk size.")
if __name__ == "__main__":
main()
Step-by-Step Explanation of the Code
1. Import os Module
Python Code
import os
os is used for file operations like checking existence (os.path.isfile) and getting file size (os.path.getsize).
2. Define the split_file Function
Python Code
def split_file(file_path, chunk_size):
Takes two parameters: the file path and the desired chunk size in bytes.
3. Error Handling
Python Code
if not os.path.isfile(file_path):
print(f"Error: File '{file_path}' does not exist.")
return
if chunk_size <= 0:
print("Error: Chunk size must be a positive number.")
return
4. Setup for Splitting
file_size: Used for progress tracking. base_name: Extracts the file name without extension (e.g., myfile from myfile.txt).
5. Read and Split the File
Opens the file in binary read mode (rb). Reads chunk_size bytes at a time in a loop until no data remains (chunk is empty).
6. Write Chunks to New Files
Python Code
output_file = f"{base_name}_part{part_num}"
with open(output_file, 'wb') as out_f:
out_f.write(chunk)
7. Progress Feedback
Python Code
bytes_processed += len(chunk)
print(f"Created: {output_file} ({bytes_processed}/{file_size} bytes processed)")
part_num += 1
Updates the number of bytes processed and increments the part number.
8. User Interaction in main
Python Code
def main():
file_path = input("Enter the path to the file you want to split: ")
try:
chunk_size = int(input("Enter the chunk size (in bytes): "))
split_file(file_path, chunk_size)
except ValueError:
print("Error: Please enter a valid number for chunk size.")
9. Run the Script
Python Code
if __name__ == "__main__":
main()
How to Use It
- Save the code as file_splitter.py.
- Create a test file (e.g., test.txt) with some content.
- Run the Script.
Example Output
For a file largefile.zip (1 MB) with chunk size 262144 (256 KB):
Python Output Code
Enter the path to the file you want to split: largefile.zip
Enter the chunk size (in bytes): 262144
Created: largefile_part1 (262144/1048576 bytes processed)
Created: largefile_part2 (524288/1048576 bytes processed)
Created: largefile_part3 (786432/1048576 bytes processed)
Created: largefile_part4 (1048576/1048576 bytes processed)
File splitting completed successfully!