Skip to content

Commit

Permalink
Create snake_case_renamer_depth_one.py
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
NitkarshChourasia authored Jul 27, 2023
1 parent 1017326 commit 4b24bf7
Showing 1 changed file with 79 additions and 0 deletions.
79 changes: 79 additions & 0 deletions snake_case_renamer_depth_one.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
import os
import argparse
from typing import Union

def generate_unique_name(directory: str, name: str) -> str:
"""
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.
"""
base_name, extension = os.path.splitext(name)
index = 1
while os.path.exists(os.path.join(directory, f"{base_name}_{index}{extension}")):
index += 1
return f"{base_name}_{index}{extension}"

def rename_files_and_folders(directory: str) -> None:
"""
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
"""
if not os.path.isdir(directory):
raise ValueError("Invalid directory path.")

for name in os.listdir(directory):
old_path = os.path.join(directory, name)
new_name = name.lower().replace(" ", "_")
new_path = os.path.join(directory, new_name)

# Check if the new filename is different from the old filename
if new_name != name:
# Check if the new filename already exists in the directory
if os.path.exists(new_path):
# If the new filename exists, generate a unique name with an index
new_path = generate_unique_name(directory, new_name)

os.rename(old_path, new_path)

def main() -> None:
"""
Main function to handle command-line arguments and call the renaming function.
Usage:
------
python script_name.py <directory_path>
Example:
--------
python rename_files_script.py /path/to/directory
"""
# Create a parser for command-line arguments
parser = argparse.ArgumentParser(description="Rename files and folders to lowercase with underscores.")
parser.add_argument("directory", type=str, help="Path to the directory containing the files and folders to be renamed.")
args = parser.parse_args()

# Call the rename_files_and_folders function with the provided directory path
rename_files_and_folders(args.directory)

if __name__ == "__main__":
main()

0 comments on commit 4b24bf7

Please sign in to comment.