Rename files and folders in the specified directory to lowercase with underscores. Parameters: ---------- directory : str The path to the directory containing the files and folders to be renamed. Returns: ------- None
(directory: str)
| 26 | |
| 27 | |
| 28 | def rename_files_and_folders(directory: str) -> None: |
| 29 | """ |
| 30 | Rename files and folders in the specified directory to lowercase with underscores. |
| 31 | |
| 32 | Parameters: |
| 33 | ---------- |
| 34 | directory : str |
| 35 | The path to the directory containing the files and folders to be renamed. |
| 36 | |
| 37 | Returns: |
| 38 | ------- |
| 39 | None |
| 40 | """ |
| 41 | if not os.path.isdir(directory): |
| 42 | raise ValueError("Invalid directory path.") |
| 43 | |
| 44 | for name in os.listdir(directory): |
| 45 | old_path = os.path.join(directory, name) |
| 46 | new_name = name.lower().replace(" ", "_") |
| 47 | new_path = os.path.join(directory, new_name) |
| 48 | |
| 49 | # Check if the new filename is different from the old filename |
| 50 | if new_name != name: |
| 51 | # Check if the new filename already exists in the directory |
| 52 | if os.path.exists(new_path): |
| 53 | # If the new filename exists, generate a unique name with an index |
| 54 | new_path = generate_unique_name(directory, new_name) |
| 55 | |
| 56 | os.rename(old_path, new_path) |
| 57 | |
| 58 | |
| 59 | def main() -> None: |
no test coverage detected