How to rename files in Windows using your Python hands in 2022?

How to rename files in Windows using your Python hands in 2022?

Rename the windows file using python

11_Vipul kunwar's photo
11_Vipul kunwar

Published on Dec 23, 2021

8 min read

Subscribe to my newsletter and never miss my upcoming articles

Table of contents

  • Import the os
  • Run the above code

image.png

Do you know why I've used windows in the headline?
So, you said, " No!!".

Ok, there are two reasons behind it.

First, I don't want to waste your time. Second, I'm using windows.

Let's come back to the first reason. The first reason is to save your time.

As the headline suggests, you've to rename the files. So, here I'm using a windows directory structure. If you use Mac or Linux, the directory structure on your computer may differ. Although, python is the same for all operating systems. But I want to be as specific as possible. Not general.

And the second one is obvious that I'm using windows. So, I'll talk about windows.
Not stupidly talk about Linux or Mac if I don't know about it.

Import the os

First of all, we've to rename the files of windows. That means we've to communicate with the Operating System(os). That's why we've to import the os using the import statement. ↓

import os

As you know, we've to rename the files in the specific directory. And in this directory, there can be a bunch of files. That means we've to rename the many files. Hence, you need to repeat the task of renaming every file.

For this reason, I'm going to use the function. So, I'm going to define a function below ↓

def main():

Next, I'm going to use i = 0. The i = 0 shows that renaming of the file starting from the first file in the last name of the path.

def main():
    i = 0

Define the file path

I'm also going to define the path of the directory.

path = "C:/Users/HP/Downloads/h/"

## Use the for-loop in python

Next, I'm going to use the for loop inside the function. The for loop iterates each file name in the directory.
Meanwhile, I'm using os.listdir(path) that gets all the files in that directory.

for filename in os.listdir(path):

Create the source and destination file in windows

Inside for-loop, I'm going to create my_dest variable.
This variable stores every new file name in the for-loop.

for filename in os.listdir(path):
        my_dest  = "img"+str(i)+".jpg"

When I combine all the above, code it becomes:

import os
# Create a function to rename the files
def main():
     i = 0
     path = "C:/Users/HP/Downloads/h/"
     for filename in os.listdir(path):
         # the format that I've to save the file
         my_dest  = "img"+str(i)+".jpg"

Here, you can see that I've created the destination filenames that are new file names. But to rename the filenames, you need the source file. So, you can do this inside the for-loop.

my_source =path + filename

We've created a new name for the old filenames and defined the old filenames.
Next and most important, we've to combine new filenames with the path variable. That means we're creating new filenames with the same path.

Thus, you've to add the below line of code:

my_dest =path + my_dest

We've done pretty much work. So, take a rest!!

I hope you've taken enough rest. Let's use our brains.

Till here, we've done all the things such as showing the source & destination filenames.

After this, I'm going to apply the method. That will rename the file name in the directory. This will be os.rename(my_source, my_dest).

You can see that the my_source filename variable renamed with my_dest file names. Also, this line of code has indented inside for loop.

When you add this code of lines in your program:

import os
# Create a function to rename the files
def main():
     i = 0
     path = "C:/Users/HP/Downloads/h/"
     for filename in os.listdir(path):
         # the format that I've to save the file
         my_dest  = input("img"+str(i)+".jpg")
         # rename the file name using os.rename() method
         os.rename(my_source, my_dest)

Increment i by 1 inside the python loop

When the program run, python renames the first file. After that, it has to go to another file. For that, you've to increment the value from i = 0. So, you can use i += 1.

I'm going to add this line of code to my program.

import os
# Create a function to rename the files
def main():
     i = 0
     path = "C:/Users/HP/Downloads/h/"
     for filename in os.listdir(path):
         # the format that I've to save the file
         my_dest  = input("img"+str(i)+".jpg")
         # rename the file name using os.rename() method
         os.rename(my_source, my_dest)
         # go to the next file
         i += 1

At last, to run your program, we're going to call the function. To do this, you've to use the below line of code.

if __name__=="__main__":
    main()

So, we've done pretty much work. The final code is

import os
# Create a function to rename the files
def main():
     i = 0
     path = "C:/Users/HP/Downloads/h/"
     for filename in os.listdir(path):
         # the format that I've to save the file
         my_dest  = "img"+str(i)+".jpg"
         # rename the file name using os.rename() method
         os.rename(my_source, my_dest)
         # go to the next file
         i += 1

if __name__=="__main__":
    main()

Here __name__ == "__main__" showing that python will run current program.

Run the above code

When we run the above code, python will rename our filenames. To check this out, First, you've to go to your computer files. And go to the path that we've defined.

Note: Your file path could be different. I'm using the path that I've defined.
So, the path = "C:/Users/HP/Downloads/h/".

Next, I'm going to take you on this path. So, Let's go in shorts. Because Inside computer could be HOT.

First we're going to the "C:/Users/HP/Downloads".

image.png

So, you're seeing many houses of the folder. But in which of the folder you'll go? According to our path, i.e. "C:/Users/HP/Downloads/h/,. we've to go to the h folder.

Let's go inside the h folder.

image.png

So, we can see that our filenames replaced as img0, img1, img2.

But you know, these img0, img1 and img2 are boring. Let's make it excited. Can you guess what exciting I'm going to do? Yes, you're right!!! I'm going to rename these filenames to a cool one.

OK! You demand coolness. So, you can do this by changing the one line of code in program. The line is below:

        my_dest  = "img"+str(i)+".jpg"

So, you've understood this line showing the renamed filenames. Hey I'm going to tweak it. Not from my hands. But from my python hands. After, tweaking this my code line looks as,

       my_dest  =input("Put the file name you want: "+str(i))

When you run this into your code your program looks like

import os
# Create a function to rename the files
def main():
     i = 0
     path = "C:/Users/HP/Downloads/h/"
     for filename in os.listdir(path):
         # the format that I've to save the file
         my_dest  = "img"+str(i)+".jpg"
         # rename the file name using os.rename() method
         os.rename(my_source, my_dest)
         # go to the next file
         i += 1

if __name__=="__main__":
    main()

Let's run our code:

Python 3.7.9 (bundled)
>>> %Run rename.py
Put the file name you want: 0Do
Put the file name you want: 1Pretty
Put the file name you want: 2Shit

Here, you can see that I've putted the 3 input. Do name for the first file. Pretty for the second file. Shit for the third file.

Thus, I'm going to check these files again:

image.png

So, you can see that img0, img1 & img2 has changed to the Do, Pretty, Shit. At last, you've seen the Program to rename the windows file. Oh... here's the cold.

I'm in pyjamas and finished the python blogs. The last thing I've to do is to wrap it all together.

Wrap it all together

At last, you've seen how to rename the files in windows using python. To do this, you need the os. So, I've imported the operating system(os) module. We've done this because the files are present in os.

Meanwhile, you can define the function. The function renaming the Multiple files. Inside the function, you've to define the path of the current directory.

You can see that I've used the for-loop. That will iterate each filename in os. To rename the file in python first, I'd defined the my_source as a path of my files. Next, I'd define the my_dest variable that stores the rename of the file names.

After that, we've to use os.rename() method. That will rename the file name from my_source to my_dest.

Here, you can observe that after renaming the first file, you've to rename another file. As a result, you've to increment variable i by 1.

The last thing you've to do is use if __name__=="__main__":. That'll run the current program. Inside this, you've to call the function name.

When python runs the program, then python takes input from you. You've to put your new names for files. And when you check the directory, your files exist renamed.

Thus, what do you feel about this blog post? Comment below. Also, subscribe to my newsletter. So, you can get crispy python blogs.

Ignoring this blog post can Impact your python skills. When you Implement this project, You'll learn about methods and functions. As a result, It'll give you benefit in another project. I want you to go step by step in this project.

I know you want to connect with me. For that, you've to walk here

Don't forget to share!!

Next, you've to go below. Below you'll see three boxes for three blog posts. One of them is a python project for GitHub. After finishing, this project you've to go straight to the below project.

If you finished the below project, check out the other articles. Also, you can give feedback on this project.

Check out my other articles below
 
Share this