MCPcopy Create free account
hub / github.com/geekcomputers/Python / generate_unique_name

Function generate_unique_name

snake_case_renamer_depth_one.py:5–25  ·  view source on GitHub ↗

Generate a unique name for a file or folder in the specified directory. Parameters: ---------- directory : str The path to the directory. name : str The name of the file or folder. Returns: ------- str The unique name with an index.

(directory: str, name: str)

Source from the content-addressed store, hash-verified

3
4
5def generate_unique_name(directory: str, name: str) -> str:
6 """
7 Generate a unique name for a file or folder in the specified directory.
8
9 Parameters:
10 ----------
11 directory : str
12 The path to the directory.
13 name : str
14 The name of the file or folder.
15
16 Returns:
17 -------
18 str
19 The unique name with an index.
20 """
21 base_name, extension = os.path.splitext(name)
22 index = 1
23 while os.path.exists(os.path.join(directory, f"{base_name}_{index}{extension}")):
24 index += 1
25 return f"{base_name}_{index}{extension}"
26
27
28def rename_files_and_folders(directory: str) -> None:

Callers 1

rename_files_and_foldersFunction · 0.85

Calls

no outgoing calls

Tested by

no test coverage detected