Skip to content

Commit 4b24bf7

Browse files
Create snake_case_renamer_depth_one.py
Sure! Below is the generated `README.md` file for the program along with some explanations. The name of the file should be `rename_files_script.py` to reflect the purpose of the script. --- # File and Folder Renamer A Python script to rename files and folders in a specified directory to lowercase with underscores. ## Usage To use the script, run the following command in your terminal: ``` python rename_files_script.py <directory_path> ``` Replace `<directory_path>` with the path to the directory containing the files and folders you want to rename. ## Features - Converts file and folder names to lowercase. - Replaces spaces with underscores. - Ensures that each name is unique in the directory by appending an index if necessary. ## Prerequisites - Python 3.x - `argparse` module (included in the standard library) ## How It Works The script takes a directory path as a command-line argument and iterates through all the files and folders in the directory. For each item, it generates a new name by converting it to lowercase and replacing spaces with underscores. If the new name is different from the original name and already exists in the directory, the script generates a unique name by appending an index to the new name. The script then renames the item with the new name and continues the process for all items in the directory. ## Example Let's say we have a directory called `example_dir` with the following files and folders: ``` example_dir/ File 1.txt File 2.txt File 3.txt Folder 1/ Folder 2/ Folder 3/ ``` After running the script with the command: ``` python rename_files_script.py example_dir ``` The directory will be renamed as follows: ``` example_dir/ file_1.txt file_2.txt file_3.txt folder_1/ folder_2/ folder_3/ ``` All file and folder names have been converted to lowercase with underscores, and the names are unique within the directory. --- Remember to replace the `<directory_path>` placeholder in the README with the actual path to your directory when you distribute the script. The README provides clear instructions on how to use the script and explains its features and functionality.
1 parent 1017326 commit 4b24bf7

File tree

1 file changed

+79
-0
lines changed

1 file changed

+79
-0
lines changed

snake_case_renamer_depth_one.py

+79
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
import os
2+
import argparse
3+
from typing import Union
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+
def rename_files_and_folders(directory: str) -> None:
28+
"""
29+
Rename files and folders in the specified directory to lowercase with underscores.
30+
31+
Parameters:
32+
----------
33+
directory : str
34+
The path to the directory containing the files and folders to be renamed.
35+
36+
Returns:
37+
-------
38+
None
39+
"""
40+
if not os.path.isdir(directory):
41+
raise ValueError("Invalid directory path.")
42+
43+
for name in os.listdir(directory):
44+
old_path = os.path.join(directory, name)
45+
new_name = name.lower().replace(" ", "_")
46+
new_path = os.path.join(directory, new_name)
47+
48+
# Check if the new filename is different from the old filename
49+
if new_name != name:
50+
# Check if the new filename already exists in the directory
51+
if os.path.exists(new_path):
52+
# If the new filename exists, generate a unique name with an index
53+
new_path = generate_unique_name(directory, new_name)
54+
55+
os.rename(old_path, new_path)
56+
57+
def main() -> None:
58+
"""
59+
Main function to handle command-line arguments and call the renaming function.
60+
61+
Usage:
62+
------
63+
python script_name.py <directory_path>
64+
65+
Example:
66+
--------
67+
python rename_files_script.py /path/to/directory
68+
69+
"""
70+
# Create a parser for command-line arguments
71+
parser = argparse.ArgumentParser(description="Rename files and folders to lowercase with underscores.")
72+
parser.add_argument("directory", type=str, help="Path to the directory containing the files and folders to be renamed.")
73+
args = parser.parse_args()
74+
75+
# Call the rename_files_and_folders function with the provided directory path
76+
rename_files_and_folders(args.directory)
77+
78+
if __name__ == "__main__":
79+
main()

0 commit comments

Comments
 (0)