Multivolume File Wrapper

0.2.3 · active · verified Thu Apr 09

multivolumefile is a Python library that provides a file-like object wrapper to automatically split large files into multiple smaller 'volumes' during writing, and merge them back seamlessly during reading. It handles the underlying file operations, creating numbered volume files (e.g., `filename.000`, `filename.001`). The current version is 0.2.3, and it has a moderate release cadence, with several minor updates addressing stability and feature enhancements.

Warnings

Install

Imports

Quickstart

This quickstart demonstrates how to write data to a `MultivolumeFile` that automatically splits content into specified `volume_size` chunks, and then how to read the entire content back seamlessly. It also includes cleanup for the generated volume files.

from multivolume import MultivolumeFile
import os

# Define a base filename for our multivolume file
base_filename = "my_test_multi_file"

# --- Writing a multivolume file ---
# The file will be split into volumes of 1024 bytes each
with MultivolumeFile(base_filename, "wb", volume_size=1024) as f:
    f.write(b"Hello world. This is the first line.\n")
    # This line will likely span across volumes or start a new one
    f.write(b"This is a longer line that ensures multiple volumes are created if content is sufficient.\n")
    f.write(b"End of content.\n")

print(f"Successfully wrote content to multivolume file(s) starting with '{base_filename}'.")

# --- Reading a multivolume file ---
# Open using the same base filename
with MultivolumeFile(base_filename, "rb") as f:
    read_content = f.read()
    print("\n--- Read content ---")
    print(read_content.decode('utf-8'))

# --- Cleanup (optional) ---
# Remove the generated volume files
import glob
for f_path in glob.glob(f"{base_filename}.*"):
    os.remove(f_path)
print(f"\nCleaned up files matching '{base_filename}.*'")

view raw JSON →