Skip to main content

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 Path or str for 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 (str or Path): 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, length n (default 6)
    • {uuid[:n]}: UUID4 hex string truncated to n chars (default 32)
  • verbose (bool, default False): Enable logging of attempts and results.

  • if_exists_only (bool, default True): Only add suffix if base path exists.

  • return_str (bool, default False): Return a string path instead of a Path object.

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

uniqpath-0.1.0.tar.gz (5.8 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

uniqpath-0.1.0-py3-none-any.whl (5.0 kB view details)

Uploaded Python 3

File details

Details for the file uniqpath-0.1.0.tar.gz.

File metadata

  • Download URL: uniqpath-0.1.0.tar.gz
  • Upload date:
  • Size: 5.8 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: uv/0.7.3

File hashes

Hashes for uniqpath-0.1.0.tar.gz
Algorithm Hash digest
SHA256 2f2b358a766384fe8129a9c3f6ec11552636d543199892a08c1a520b1e47252c
MD5 e9230353b7c14e3eeb3749d163d608aa
BLAKE2b-256 c4bfc6e28c0b684ac1d06c59ae48d6857b63d017012e7a31d92a279ec3b6f33e

See more details on using hashes here.

File details

Details for the file uniqpath-0.1.0-py3-none-any.whl.

File metadata

  • Download URL: uniqpath-0.1.0-py3-none-any.whl
  • Upload date:
  • Size: 5.0 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: uv/0.7.3

File hashes

Hashes for uniqpath-0.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 30d3bea307d8610f8473f878ab101d027230cd2d0c86ab81f4540e2e2d3fec7b
MD5 18a6c334e243d5cf22c892dee93d2f09
BLAKE2b-256 b84cbedb3b377195d3f90e259d5e249aaaa842e109e87f0e23ce27d570f1c49b

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page