backports-tempfile

1.0 · maintenance · verified Sat Apr 11

This package provides backports of new features found in Python's standard `tempfile` module, primarily `tempfile.TemporaryDirectory` (introduced in Python 3.4), allowing its use in older Python environments, including Python 2.7, 3.4, 3.5, and 3.6. It is currently at version 1.0, with its last release in October 2017, indicating a very slow or ceased release cadence given its backport nature and the progression of Python versions.

Warnings

Install

Imports

Quickstart

Demonstrates how to use `TemporaryDirectory` from the `backports.tempfile` package as a context manager to create and automatically clean up a temporary directory.

from backports import tempfile
import os

# Create a temporary directory using the backported feature
with tempfile.TemporaryDirectory() as temp_dir:
    print(f"Created temporary directory: {temp_dir}")
    # Example: create a file inside the temporary directory
    file_path = os.path.join(temp_dir, "my_temp_file.txt")
    with open(file_path, "w") as f:
        f.write("Hello from tempfile backport!")
    print(f"File created at: {file_path}")
    print(f"Contents of {file_path}: {open(file_path).read()}")

# At this point, temp_dir and its contents should be automatically removed

view raw JSON →