When they're not builtins, they're boltons.
—
Quality
Pending
Does it follow best practices?
Impact
Pending
No eval scenarios have been run
Atomic file operations, advanced I/O utilities, path manipulation, and file system operations. Includes spooled I/O that automatically switches to disk for large data, atomic file saving with backup support, and comprehensive path manipulation utilities.
Safe file writing operations that prevent data corruption.
def atomic_save(dest_path, **kwargs):
"""
Atomic file saving with backup options.
Parameters:
- dest_path (str): Destination file path
- text_mode (bool): Open in text mode (default: True)
- backup (bool): Create backup before overwrite
- part_file (str): Temporary file suffix
Returns:
AtomicSaver: Context manager for atomic operations
"""
class AtomicSaver:
"""Context manager for atomic file writing operations."""
def __init__(self, dest_path, **kwargs): ...
def __enter__(self): ...
def __exit__(self, exc_type, exc_val, exc_tb): ...Directory and file system manipulation utilities.
def mkdir_p(path):
"""
Create directory including parent directories (like mkdir -p).
Parameters:
- path (str): Directory path to create
Returns:
None
"""
def copy_tree(src, dst, symlinks=False, ignore=None):
"""
Copy directory trees.
Parameters:
- src (str): Source directory
- dst (str): Destination directory
- symlinks (bool): Copy symlinks as symlinks
- ignore (callable): Function to ignore certain files
Returns:
None
"""
def iter_find_files(directory, patterns, **kwargs):
"""
Iterator for finding files matching patterns.
Parameters:
- directory (str): Directory to search
- patterns (list): List of glob patterns
Yields:
str: Matching file paths
"""Advanced path manipulation and normalization.
def path_to_unicode(path):
"""
Convert path to unicode string.
Parameters:
- path: Path to convert
Returns:
str: Unicode path string
"""
def augpath(path, suffix='', prefix='', ext=None, base=None, dpath=None, multidot=False):
"""
Augment path with various components.
Parameters:
- path (str): Base path
- suffix (str): Suffix to add to filename
- prefix (str): Prefix to add to filename
- ext (str): New extension (with or without dot)
- base (str): New basename for file
- dpath (str): New directory path
- multidot (bool): Handle multiple extensions
Returns:
str: Augmented path
"""
def shrinkuser(path, home='~'):
"""
Contract user home directory in path.
Parameters:
- path (str): Path to contract
- home (str): Home directory representation
Returns:
str: Path with contracted home directory
"""
def expandpath(path):
"""
Expand environment variables and user directory in path.
Parameters:
- path (str): Path to expand
Returns:
str: Expanded path
"""Memory-efficient I/O that spools to disk for large data.
class SpooledIOBase(IOBase):
"""Abstract base for spooled I/O implementations."""
def __init__(self, max_size=5000000, dir=None): ...
def rollover(self): ...
@property
def rolled(self): ...
class SpooledBytesIO(SpooledIOBase):
"""In-memory bytes I/O that spools to disk when large."""
def write(self, s): ...
def read(self, n=-1): ...
def seek(self, pos, whence=0): ...
def tell(self): ...
class SpooledStringIO(SpooledIOBase):
"""In-memory text I/O that spools to disk when large."""
def write(self, s): ...
def read(self, n=-1): ...
def readline(self, length=None): ...
class MultiFileReader:
"""Read from multiple file objects as single stream."""
def __init__(self, file_objects): ...
def read(self, size=-1): ...
def readline(self): ...
def close(self): ...Miscellaneous file handling utilities.
def is_text_fileobj(fileobj):
"""
Check if file object is in text mode.
Parameters:
- fileobj: File object to check
Returns:
bool: True if file object is in text mode
"""
class FilePerms:
"""File permissions representation and manipulation."""
def __init__(self, perms): ...
@property
def user(self): ...
@property
def group(self): ...
@property
def other(self): ...
def __str__(self): ...
class DummyFile:
"""File-like object that discards all writes."""
def write(self, data): ...
def flush(self): ...
def close(self): ...from boltons.fileutils import atomic_save, mkdir_p, augpath
from boltons.ioutils import SpooledBytesIO, MultiFileReader
from boltons.pathutils import shrinkuser, expandpath
# Atomic file writing
with atomic_save('/path/to/important.txt') as f:
f.write('Critical data')
# File is only written if no exceptions occur
# Create directories safely
mkdir_p('/path/to/nested/directories')
# Path manipulation
original = '/home/user/documents/file.txt'
backup = augpath(original, suffix='_backup', ext='.bak')
print(backup) # '/home/user/documents/file_backup.bak'
# Contract and expand paths
short_path = shrinkuser('/home/user/file.txt')
print(short_path) # '~/file.txt'
full_path = expandpath('~/file.txt')
print(full_path) # '/home/user/file.txt'
# Spooled I/O for memory efficiency
spooled = SpooledBytesIO(max_size=1024*1024) # 1MB threshold
spooled.write(b'small data') # Stays in memory
spooled.write(large_data) # Spools to disk when threshold exceeded
# Read from multiple files as one stream
with open('file1.txt', 'rb') as f1, open('file2.txt', 'rb') as f2:
multi_reader = MultiFileReader([f1, f2])
combined_data = multi_reader.read()# Constants
FULL_PERMS = 0o777 # Full read/write/execute permissions
RW_PERMS = 438 # Read/write permissions (octal 666)
READ_CHUNK_SIZE = 21333 # Default chunk size for reading operationsInstall with Tessl CLI
npx tessl i tessl/pypi-boltons