Below are two approaches to reassemble your original file, depending on how you originally split it.
Approach 1: Using RAR to Rejoin a Multi-Volume Archive
If you split your file using RAR (i.e. you created a multi‑volume archive like m.bin.rar, m.bin.r00, m.bin.r01, …), you can use the RAR extraction command to reassemble the original file. For example, if you’ve already downloaded the RAR binary in your Colab environment (e.g., at ./rar/rar), you can run:
import subprocess
# Command to extract the multi-volume archive.
# This will reassemble m.bin from m.bin.rar and its parts.
command = "./rar/rar x m.bin.rar"
# Execute the command and capture the outputs for debugging.
result = subprocess.run(command, shell=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
print("STDOUT:")
print(result.stdout.decode())
print("STDERR:")
print(result.stderr.decode())
Explanation:
- The command
rar x m.bin.rar tells RAR to extract files from the archive.
- RAR will automatically detect and join the associated multi‑volume parts (e.g.,
m.bin.r00, m.bin.r01, etc.) and reconstruct m.bin (or the file contained inside the archive) in the current folder.
- Make sure all parts are located in the same directory.
Approach 2: Joining File Splits using Pure Python
If you used a pure Python method to split the file into chunks (for example, naming them m.bin.part000, m.bin.part001, …), you can join them back together by reading each part in order and writing them to a new file:
import glob
def join_file_parts(base_filename, output_filename):
# Create a list of parts matching the pattern and sort them.
parts = sorted(glob.glob(f"{base_filename}.part*"))
with open(output_filename, "wb") as outfile:
for part in parts:
with open(part, "rb") as infile:
outfile.write(infile.read())
print(f"Reassembled file saved as: {output_filename}")
# Replace "m.bin" with your base name if different.
join_file_parts("m.bin", "m.bin.reassembled")
Explanation:
- We use the
glob module to get all file parts that match the naming pattern.
- The files are sorted (assuming they were named with a numeric or lexicographical order, e.g.
part000, part001, …).
- Each part is read in binary mode and written in order to a new file.
Choose the method that matches how you originally performed the split. If you used RAR to archive and split your file, use . If you split the file using a custom Python script, then will reassemble your original file.