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

Function rename_files_and_folders

snake_case_renamer_depth_one.py:28–56  ·  view source on GitHub ↗

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)

Source from the content-addressed store, hash-verified

26
27
28def 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
59def main() -> None:

Callers 1

mainFunction · 0.85

Calls 1

generate_unique_nameFunction · 0.85

Tested by

no test coverage detected