=============== LIBRARY RULES =============== From library maintainers: - Import from unused_path: unused_filename and unused_directory. The default suffix format is 'name (1).ext'. - Pass create=True to create the file or directory atomically and avoid races between threads or processes. ### Install unused-path with pip Source: https://github.com/ysskrishna/unused-path/blob/main/README.md Install the unused-path package from PyPI using pip. ```bash pip install unused-path ``` -------------------------------- ### Custom formatting with formatter parameter Source: https://github.com/ysskrishna/unused-path/blob/main/README.md Customize the numeric suffix format. The formatter receives base, extension (for files), and the attempt number. Examples show zero-padding and underscore styles. ```python from unused_path import unused_filename, unused_directory # Version numbers with zero-padding formatter = lambda base, ext, n: f"{base}_v{n:03d}{ext}" path = unused_filename("log.txt", formatter=formatter) # → 'log_v001.txt', 'log_v002.txt', ... # Underscore style formatter = lambda base, ext, n: f"{base}_{n}{ext}" path = unused_filename("data.csv", formatter=formatter) # → 'data_1.csv', 'data_2.csv', ... # Similar formatting for directories formatter = lambda base, n: f"{base}_v{n}" dir_path = unused_directory("backup", formatter=formatter) # → 'backup_1', 'backup_2', ... ``` -------------------------------- ### Custom Formatter with Timestamp: unused_filename formatter Source: https://github.com/ysskrishna/unused-path/blob/main/README.md Uses a custom formatter function to generate unique filenames with a timestamp. The formatter receives base, ext, and n, and returns a string. The example shows a call that produces a filename like 'log_20240115_143052_1.txt'. ```python from unused_path import unused_filename from datetime import datetime def timestamp_formatter(base, ext, n): timestamp = datetime.now().strftime("%Y%m%d_%H%M%S") return f"{base}_{timestamp}_{n}{ext}" path = unused_filename("log.txt", formatter=timestamp_formatter) # → 'log_20240115_143052_1.txt' ``` -------------------------------- ### Thread-safe creation with create=True Source: https://github.com/ysskrishna/unused-path/blob/main/README.md Atomically creates the file or directory to prevent race conditions in multi-threaded or concurrent environments. The created directory is empty and owned by the calling process. ```python from unused_path import unused_filename, unused_directory # Atomically creates the file — no race conditions! path = unused_filename("download.zip", create=True) with open(path, 'wb') as f: f.write(data) # Same for directories export_dir = unused_directory("exports", create=True) ``` -------------------------------- ### Backup System: unused_directory with create=True Source: https://github.com/ysskrishna/unused-path/blob/main/README.md Creates a backup directory with a timestamp-based name using unused_directory with create=True. The timestamp is formatted as YYYYMMDD. ```python from datetime import datetime from unused_path import unused_directory def create_backup(): timestamp = datetime.now().strftime("%Y%m%d") backup_dir = unused_directory(f"backup_{timestamp}", create=True) # ... perform backup ... return backup_dir ``` -------------------------------- ### Temporary Files: unused_filename with tempfile Source: https://github.com/ysskrishna/unused-path/blob/main/README.md Creates a temporary file in the system temp directory using unused_filename with create=True. Combines tempfile.gettempdir() and os.path.join. ```python from unused_path import unused_filename import tempfile import os def create_temp_file(prefix="temp"): temp_dir = tempfile.gettempdir() path = os.path.join(temp_dir, f"{prefix}.txt") return unused_filename(path, create=True) ``` -------------------------------- ### Download Manager: unused_filename with create=True Source: https://github.com/ysskrishna/unused-path/blob/main/README.md Downloads a file from a URL and saves it to a unique filename using unused_filename with create=True. The filename is derived from the URL's last segment. ```python import requests from unused_path import unused_filename def download_file(url): filename = url.split('/')[-1] path = unused_filename(filename, create=True) response = requests.get(url) with open(path, 'wb') as f: f.write(response.content) return path ``` -------------------------------- ### Basic usage of unused_filename and unused_directory Source: https://github.com/ysskrishna/unused-path/blob/main/README.md Generate unused filenames and directory names. If the path exists, a numeric suffix is automatically appended (e.g., 'document (1).pdf'). ```python from unused_path import unused_filename, unused_directory # Generate unused filename path = unused_filename("document.pdf") print(path) # 'document.pdf' (if available) # If file exists, automatically increments path = unused_filename("document.pdf") print(path) # 'document (1).pdf' # Same for directories dir_path = unused_directory("backup") print(dir_path) # 'backup' or 'backup (1)' if exists ``` -------------------------------- ### Limiting Maximum Tries: unused_filename max_tries Source: https://github.com/ysskrishna/unused-path/blob/main/README.md Attempts to generate a unique filename with a maximum of 100 tries. Raises RuntimeError if too many files with the same name exist, which is caught and handled. ```python from unused_path import unused_filename try: path = unused_filename("file.txt", max_tries=100) except RuntimeError: print("Too many files with the same name!") ``` -------------------------------- ### unused_directory(path, *, formatter=None, max_tries=10000, create=False) Source: https://github.com/ysskrishna/unused-path/blob/main/README.md Generates an unused directory name by appending a numeric suffix if the given path already exists. Supports custom formatting and atomic creation for thread-safe use. ```APIDOC ## unused_directory(path, *, formatter=None, max_tries=10000, create=False) ### Description Generates an unused directory name by appending a numeric suffix if the given path already exists. Supports custom formatting and atomic creation for thread-safe use. ### Parameters - `path` (str) - Required - Desired directory path (relative or absolute) - `formatter` (callable) - Optional - Custom formatter function with signature `(base: str, n: int) -> str` - `max_tries` (int) - Optional - Maximum attempts to find unused name (default: 10,000) - `create` (bool) - Optional - If True, atomically creates the directory to prevent race conditions ### Returns - Unused path (str) - preserves the absolute or relative format of the input ### Raises - `RuntimeError` - If no unused path is found within max_tries attempts - `OSError` - If directory creation fails when create=True (e.g., permission denied) ### Request Example ```python from unused_path import unused_directory # Generate unused directory name dir_path = unused_directory("backup") print(dir_path) # 'backup' or 'backup (1)' if exists ``` ### Custom Formatting Example ```python from unused_path import unused_directory # Similar formatting for directories formatter = lambda base, n: f"{base}_v{n}" dir_path = unused_directory("backup", formatter=formatter) # → 'backup_1', 'backup_2', ... ``` ``` -------------------------------- ### Data Export: unused_filename with create=True Source: https://github.com/ysskrishna/unused-path/blob/main/README.md Exports a DataFrame to a CSV file with a unique filename using unused_filename with create=True. Prints a confirmation message with the path. ```python from unused_path import unused_filename def export_to_csv(data, base_name="export"): path = unused_filename(f"{base_name}.csv", create=True) data.to_csv(path, index=False) print(f"✓ Exported to {path}") return path ``` -------------------------------- ### unused_filename(path, *, formatter=None, max_tries=10000, create=False) Source: https://github.com/ysskrishna/unused-path/blob/main/README.md Generates an unused filename by appending a numeric suffix if the given path already exists. Supports custom formatting and atomic creation for thread-safe use. ```APIDOC ## unused_filename(path, *, formatter=None, max_tries=10000, create=False) ### Description Generates an unused filename by appending a numeric suffix if the given path already exists. Supports custom formatting and atomic creation for thread-safe use. ### Parameters - `path` (str) - Required - Desired file path (relative or absolute) - `formatter` (callable) - Optional - Custom formatter function with signature `(base: str, ext: str, n: int) -> str` - `max_tries` (int) - Optional - Maximum attempts to find unused name (default: 10,000) - `create` (bool) - Optional - If True, atomically creates the file to prevent race conditions ### Returns - Unused path (str) - preserves the absolute or relative format of the input ### Raises - `RuntimeError` - If no unused path is found within max_tries attempts - `OSError` - If file creation fails when create=True (e.g., permission denied) ### Request Example ```python from unused_path import unused_filename # Generate unused filename path = unused_filename("document.pdf") print(path) # 'document.pdf' (if available) # If file exists, automatically increments path = unused_filename("document.pdf") print(path) # 'document (1).pdf' ``` ### Custom Formatting Example ```python from unused_path import unused_filename # Version numbers with zero-padding formatter = lambda base, ext, n: f"{base}_v{n:03d}{ext}" path = unused_filename("log.txt", formatter=formatter) # → 'log_v001.txt', 'log_v002.txt', ... # Underscore style formatter = lambda base, ext, n: f"{base}_{n}{ext}" path = unused_filename("data.csv", formatter=formatter) # → 'data_1.csv', 'data_2.csv', ... ``` ``` === COMPLETE CONTENT === This response contains all available snippets from this library. No additional content exists. Do not make further requests.