35 lines
1.2 KiB
Python
Executable File
35 lines
1.2 KiB
Python
Executable File
#!/usr/bin/env python
|
|
|
|
import os
|
|
|
|
def rename_folders_and_files(directory):
|
|
# First, rename files and directories in subdirectories
|
|
for root, dirs, files in os.walk(directory):
|
|
for d in dirs:
|
|
rename_folders_and_files(os.path.join(root, d))
|
|
|
|
# Then, rename files in the current directory
|
|
for root, dirs, files in os.walk(directory):
|
|
for f in files:
|
|
old_path = os.path.join(root, f)
|
|
new_path = os.path.join(root, f.lower().replace(" ", "_"))
|
|
os.rename(old_path, new_path)
|
|
|
|
# Finally, rename directories in the current directory
|
|
for root, dirs, _ in os.walk(directory):
|
|
for d in dirs:
|
|
old_path = os.path.join(root, d)
|
|
new_path = os.path.join(root, d.lower().replace(" ", "_"))
|
|
os.rename(old_path, new_path)
|
|
|
|
if __name__ == "__main__":
|
|
root_directory = os.getcwd()
|
|
confirmation = input(f"Are you sure you want to rename all files and folders in '{root_directory}'? (yes/no): ")
|
|
|
|
if confirmation.lower() == 'yes':
|
|
rename_folders_and_files(root_directory)
|
|
print("Renaming completed.")
|
|
else:
|
|
print("Operation canceled.")
|
|
|