Generate unique file or directory paths with flexible formatting.
Project description
uniqpath
Generate unique file or directory paths by appending customizable suffixes.
Description
uniqpath is a lightweight Python utility to generate unique file or directory paths by adding formatted suffixes.
It helps avoid overwriting existing files by incrementing numeric suffixes or adding randomized strings, UUIDs, or
timestamps.
Features
- Incremental numeric suffix
{num}starting at 1 - Timestamp suffix
{timestamp}(UNIX epoch seconds) - Random alphanumeric suffix
{rand[:n]}with customizable length (default 6) - UUID suffix
{uuid[:n]}truncated to customizable length (default 32) - Option to add suffix only if original path exists
- Returns either
Pathorstrfor integration flexibility - Simple and dependency-free (only standard library)
- Designed for files and directories
Installation
pip install uniqpath
Usage
from uniqpath import unique_path
# Basic usage with incremental number suffix
p1 = unique_path("output.txt") # -> output_1.txt if output.txt exists
# Custom suffix with random string of length 8
p2 = unique_path("log.txt", suffix_format="_{rand:8}")
# Use UUID suffix truncated to 6 characters
p3 = unique_path("data", suffix_format="_{uuid:6}")
# Add suffix only if the path exists, otherwise return original path
p4 = unique_path("report.txt", if_exists_only=True)
# Get result as string instead of Path
p5 = unique_path("file.txt", return_str=True)
Parameters
-
path (
strorPath): The base file or directory path. -
suffix_format (
str, default_{num}): Suffix pattern with placeholders:{num}: incrementing number starting from 1{timestamp}: UNIX timestamp in seconds{rand[:n]}: random alphanumeric string, lengthn(default 6){uuid[:n]}: UUID4 hex string truncated tonchars (default 32)
-
verbose (
bool, defaultFalse): Enable logging of attempts and results. -
if_exists_only (
bool, defaultTrue): Only add suffix if base path exists. -
return_str (
bool, defaultFalse): Return a string path instead of aPathobject.
Notes
- The function does not handle concurrency and is not thread-safe.
- Date/time placeholders use current datetime but do not support full strftime syntax.
- The function works for both files and directories.
- Make sure to handle potential race conditions externally if used in parallel environments.
License
MIT License
Author
Julien Rabault — julienrabault@icloud.com
Project details
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file uniqpath-0.1.2.tar.gz.
File metadata
- Download URL: uniqpath-0.1.2.tar.gz
- Upload date:
- Size: 6.0 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: uv/0.7.3
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
7e4e49d47f5626e29a365f911ee4004f5f6927e1cb944a5647253188c7345ccf
|
|
| MD5 |
b3d55fc2af2ce5d979360efd0c828b78
|
|
| BLAKE2b-256 |
0928491e1c316c306a6b8ded1972607daafe6caaeacfa5a7ea3be03771208565
|
File details
Details for the file uniqpath-0.1.2-py3-none-any.whl.
File metadata
- Download URL: uniqpath-0.1.2-py3-none-any.whl
- Upload date:
- Size: 4.9 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: uv/0.7.3
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
129ad53da19048077518cb1ad3953a67681a9dfb5fa6190c797e2433f2adab75
|
|
| MD5 |
a91d1612550b6a1b1d0910aeace2c8df
|
|
| BLAKE2b-256 |
f84fb12ca4a118ffb86b4423a841dc789613e207b41bbe070409498b513deb3b
|