To run the file splitting script on Google Colab, you'll need to ensure the file is uploaded to the Colab environment or connected to Google Drive. Below is a step-by-step guide:
Option 1: Split File Without Compression (Simple Chunking)
This method splits the file into 64MB chunks without compression, using pure Python.
Step 1: Upload or Mount Google Drive
First, either:
-
Upload the file directly to Colab (use the file upload widget):
from google.colab import files
uploaded = files.upload()
# The file will be saved as 'your_file.bin' (replace with your filename)
OR
-
Mount Google Drive (to access files stored there):
from google.colab import drive
drive.mount('/content/drive')
# Your Drive files will be available in `/content/drive/MyDrive/`
Step 2: Split the File
Use the Python script from earlier, adjusted for Colab:
def split_file(file_path, part_size=64 * 1024 * 1024):
part_number = 1
with open(file_path, 'rb') as f:
while True:
data = f.read(part_size)
if not data:
break
part_filename = f'part{part_number}.part'
with open(part_filename, 'wb') as part_file:
part_file.write(data)
part_number += 1
# Usage:
# Replace 'your_file.bin' with your actual file path:
split_file('/content/drive/MyDrive/your_file.bin') # If using Google Drive
# OR
split_file('your_file.bin') # If uploaded via files.upload()
Step 3: Download the Split Parts
After splitting, you can download the parts to your local machine:
files.download('part1.part')
files.download('part2.part')
# Repeat for all parts (or use a loop)
Option 2: Split and Compress with RAR (Advanced)
If you want to compress and split into RAR volumes (e.g., for archival), you can install rar on Colab and use it. Here's how:
Step 1: Install RAR
!apt-get install rar # Install RAR command-line tool
Step 2: Split and Compress
import subprocess
def split_with_rar(input_file, volume_size='64m'):
subprocess.run([
'rar', 'a', f'-v{volume_size}', # Split into 64MB volumes
'archive.rar', # Output archive name
input_file # Input file
])
# Usage:
split_with_rar('/content/drive/MyDrive/your_file.bin') # Use your file path