Monty (Python Complement Library)

2026.2.18 · active · verified Mon Apr 13

Monty is a Python library that acts as a missing complement to the Python standard library. It provides supplementary useful functions, including transparent support for zipped files, various design patterns like singletons and cached_class, and other utilities to simplify common programming tasks. The current version is 2026.2.18, and it maintains an active release cadence, often aligning with needs from scientific frameworks it supports.

Warnings

Install

Imports

Quickstart

This quickstart demonstrates `monty.io.zopen`, a utility that transparently handles compressed (gzip, bzip2) or uncompressed files based on their filename extension. It functions similarly to Python's built-in `open`, abstracting away the compression details.

import monty.io

# Example: Transparently open compressed or uncompressed files
# Create a dummy gzipped file
with open('example.txt', 'w') as f:
    f.write('Hello, Monty!\n')

import gzip
with open('example.txt', 'rb') as f_in:
    with gzip.open('example.gz', 'wb') as f_out:
        f_out.writelines(f_in)

# Use monty.io.zopen to read from the gzipped file
with monty.io.zopen('example.gz', 'rt') as f:
    content = f.read()
    print(f'Content from gzipped file: {content.strip()}')

# Use monty.io.zopen to write to a bzipped file
with monty.io.zopen('output.bz2', 'wt') as f:
    f.write('This is written to a bzip2 file using monty.io.zopen.')

# Verify by reading back
with monty.io.zopen('output.bz2', 'rt') as f:
    read_content = f.read()
    print(f'Content from bzipped file: {read_content.strip()}')

import os
os.remove('example.txt')
os.remove('example.gz')
os.remove('output.bz2')

view raw JSON →