Skip to main content

Simple utility that takes a source file and a destination directory and determines a unique name for the source file in the destination directory that will not clash with any existing file.

Project description

resolve-name-clash

Say you want to copy file a.txt into directory /path/to/destination/ but if /path/to/destination/a.txt exists, you don't want to overwrite it but instead copy the file to /path/to/destination/a_1.txt. This is a simple utility that does exactly one thing: given a source filename and a destination directory, it figures out a form of the source file's name in the destination directory that doesn't clash with any existing file.

The algorithm it uses to find a unique filename is as follows. Given a path to a source file and a path to a destination directory:

  • Set src_filename to the filename (last path segment) of the path to the source file
  • If path/to/dest/src_filename does not exist, return it
  • If path/to/dest/src_filename exists:
    • Set src_file_basename to src_filename with all the extensions removed and src_file_extensions to all the extensions of src_filename
    • Successively try path/to/dest/src_file_basename + "_1" + src_file_extensions, path/to/dest/src_file_basename + "_2" + src_file_extensions, and so on until one is found that doesn't exist. Return that one.

You can use it on the command line:

$ resolve-name-clash ~/src/a.txt ~/dest/
/Users/spather/dest/a_1.txt

It's particularly useful used in a subshell with a copy or move command:

cp ~/src/a.txt $(resolve-name-clash ~/src/a.txt ~/dest)

Or you can us it in a python program:

from resolve_name_clash import resolve_name_clash

src = Path("/path/to/src/a.txt")
dest = Path("/path/to/dest")
unique_path = resolve_name_clash(src, dest)
# unique_path will be a Path object to something like /path/to/dest/a_1.txt

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

resolve-name-clash-0.1.3.tar.gz (4.4 kB view details)

Uploaded Source

Built Distribution

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

resolve_name_clash-0.1.3-py3-none-any.whl (6.0 kB view details)

Uploaded Python 3

File details

Details for the file resolve-name-clash-0.1.3.tar.gz.

File metadata

  • Download URL: resolve-name-clash-0.1.3.tar.gz
  • Upload date:
  • Size: 4.4 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/1.1.12 CPython/3.9.9 Darwin/21.0.1

File hashes

Hashes for resolve-name-clash-0.1.3.tar.gz
Algorithm Hash digest
SHA256 dea3cd9a39e209680556113e9137debb04268298b7bcaf5b83042e9b46133501
MD5 06cb013753dfe3b772d6fff8d8a57a63
BLAKE2b-256 5e59adfd169b7a236c26494643cf112825fec3edf4df0e1cade4177ebe2f04c6

See more details on using hashes here.

File details

Details for the file resolve_name_clash-0.1.3-py3-none-any.whl.

File metadata

  • Download URL: resolve_name_clash-0.1.3-py3-none-any.whl
  • Upload date:
  • Size: 6.0 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/1.1.12 CPython/3.9.9 Darwin/21.0.1

File hashes

Hashes for resolve_name_clash-0.1.3-py3-none-any.whl
Algorithm Hash digest
SHA256 4ce2cf72ebc2d454a295218ad4915dbc868595eea16783201ea1c29c49b9c671
MD5 3b3d59dee3c2a0a8f3d48b973c4335dc
BLAKE2b-256 534b62e91f452084584f5767416954279b97eba57b60ee49631fb36885ecc847

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