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)
| 3 | |
| 4 | |
| 5 | def 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 | |
| 28 | def rename_files_and_folders(directory: str) -> None: |
no outgoing calls
no test coverage detected