# Auto-Rename Files Using Python

Hi there👋, do you download videos from Youtube, and do you have a hard time renaming them since they come with the downloader tool's name appended to the name?
<center>
![image.png](https://cdn.hashnode.com/res/hashnode/image/upload/v1639311035097/Hh1StF5vX.png)
</center>
I personally use  [Y2mate](https://www.y2mate.com/en80)  and I am usually lazy going from one file to another and removing the -y2mate extension. 

Python came to my rescue and I now do monthly renaming at once with a simple running of my script in seconds.

If you have Python installed, you do not need to pip install anything! We shall be using the os module which comes pre-bundled in the standard library.
<center>
![image.png](https://cdn.hashnode.com/res/hashnode/image/upload/v1639309642549/mqVl-aQBK.png)
</center>
Here is the script! Make necessary changes to the specific folders and what you want to rename. For example, my goal was to remove "y2mate.com - "which automatically adds while downloading 

```
import os

folder_paths = ["C:/Users/Ronnie Atuhaire/Downloads/", 
                        "C:/Users/Ronnie Atuhaire/Downloads/weeks b4/",
                        "C:/Users/Ronnie Atuhaire/Downloads/Last week/", 
                        "C:/Users/Ronnie Atuhaire/Downloads/Today/",
                        "E:/", "E:/New folder/", 
                        "E:/New folder (2)/", 
                         "E:New folder (3)/", 
                         "E:/old/", "E:/Today/",
                         "C:/Users/Ronnie Atuhaire/Downloads/week b4/",
                          ]

filenames = []
text_to_remove = "y2mate.com - ".strip('')
for folder in folder_paths:
    # print(folder)
    for count, filename in enumerate(os.listdir(folder)):
        # print(count, filename)
        if text_to_remove in filename:
            filenames.append(folder + filename)
print(len(filenames))

for count, new_file in enumerate(filenames):
    # print(count, new_file)
    old_name = new_file
    new_name = new_file.replace(text_to_remove, '')
    print(count, new_name)
    os.rename(old_name, new_name)


``` 
I tried using self-descriptive variable naming and I guess there is nothing much to explain if you are already familiar with Python basics.

✔ I created a list of my folders where the script should search<br>
Note that I used a forward slash or you can use ```r``` to represent a raw string

✔I did a nested ```for loop``` to loop through the folders using ```enumerate()``` and ```os.listdir()``` <br>
```enumerate()``` allows us to iterate through a sequence but it keeps track of both the index and the element. 

The ```enumerate()``` function takes in an iterable as an argument, such as a list, string, tuple, or dictionary.

```os.listdir()``` method in python is used to get the list of all files and directories in the specified system directory

✔ I created another for loop and used python ```replace()``` and  ```os.rename()``` to do the file editing.

GitHub  [Repo](https://github.com/Ronlin1/automatic-file-renamer) 

That's It!<Br>
If you enjoyed reading, consider subscribing and reacting to this with love by sharing, commenting and any criticism is much welcome.

📢Follow me on  [Twitter](https://twitter.com/intent/follow?screen_name=AfroBoyUg) :

**Ronnie Atuhaire**



