Memory Tempfile

2.2.3 · active · verified Thu Apr 16

memory-tempfile (version 2.2.3) is a Python library that provides helper functions to identify and utilize paths on Linux-based operating systems where RAM-based temporary files can be created. It extends Python's standard `tempfile` module to prioritize memory-backed filesystems like `tmpfs` or `ramfs` for temporary storage, aiming to improve performance by avoiding disk I/O. The project has a stable, albeit infrequent, release history with the current version released in 2019.

Common errors

Warnings

Install

Imports

Quickstart

This example demonstrates how to create a temporary file and a temporary directory using `MemoryTempfile`, which will attempt to use a RAM-based filesystem. The resources are automatically cleaned up using context managers.

from memory_tempfile import MemoryTempfile
import os

tempfile_manager = MemoryTempfile()

# Create a temporary file in a memory-backed filesystem
with tempfile_manager.TemporaryFile(mode='w+t') as tf:
    tf.write('Hello from memory tempfile!')
    tf.seek(0)
    content = tf.read()
    print(f"Read from temp file: {content}")

# Create a temporary directory in a memory-backed filesystem
with tempfile_manager.TemporaryDirectory() as td:
    print(f"Temporary directory created at: {td}")
    # You can create files inside this directory
    file_path = os.path.join(td, 'my_data.txt')
    with open(file_path, 'w') as f:
        f.write('Data in temp directory.')
    print(f"File created in temp directory: {file_path}")
# Directory and its contents are automatically removed when exiting the 'with' block

view raw JSON →