Rename files using a for

Hi. I’m new in python. I have to rename all files in a folder called “image.0001” ,“image.0002” ,“image.0003” etc in “1” ,“2”, “3” . there are more than 200 files

i did something like this to try but it doesn’t work

import os
dir = ‘paziente_2019’ #my folder
i=[‘image.0005.png’,‘image.0006.png’,‘image.0007.png’,‘image.0008.png’,‘image.0009.png’]
j=[‘5.png’,‘6.png’,‘7.png’,‘8.png’,‘9.png’]
for p in j:
for n in i:
#print(n)
#print(j)
old_file = os.path.join(dir, n)
new_file = os.path.join(dir, p)
os.rename(old_file, new_file)

thank you

It’s very hard to tell exactly what your issue is because we aren’t seeing your code as you are running it, I suspect. Please paste formatted code in blocks flanked by triple ticks so that it retains formatting. See here and here.

Like this, which should do what you are trying to do:

import os
dir = 'paziente_2019' #my folder
i=['image.0005.png','image.0006.png','image.0007.png','image.0008.png','image.0009.png']
j=['5.png','6.png','7.png','8.png','9.png']
for indx,n in enumerate(i):
    #print(n)
    #print(indx)
    #print(j[indx])
    old_file = os.path.join(dir, n)
    new_file = os.path.join(dir, j[indx])
    os.rename(old_file, new_file)

That is confirmed to work on a Unix-style system when the code is executed in the directory where paziente_2019 also is found. Make sure you check the current working directory by running pwd in a cell in your notebook. You may have different path issues to work out if you are working on a Windows machine.

I’ve tried to keep it somewhat close to the spirit of your code. There’s much better ways to do this with Python so that you abstract out finding files that look like image.0006.png and reformat them programmatically to put together a string like 6.png, in this example case. Since you say there is 200 of them, you’ll most likely want to do that. (We’ll leave that to you since this isn’t a Jupyter issue, see below + added hints way below.) There’s also zip() if you really did want to hard code them and yet check they all match as you expect before running the code. Or to avoid use of enumerate().

Importantly, this is most likely not a Jupyter issue. If you ran this code as a traditional Python script, then I’d expect you’d encounter the same thing. Meaning that it’s probably a Python issue as you hint at with, “I’m new in python”,and thus should be directed to appropriate forums when seeking help. They’ll want to know more than, " it doesn’t work", when posting these types of questions. I already brought up that windows may have different path issues and so describing with text what you are seeing specifically is important. For example if you run pwd in your notebook cell what do you see. Or if running the code as a script, where are you putting the script.


For finding the image files, you may with to use Python’s glob module or fnmatch().

Here’s how you could make i programmatically with glob:

#i=['image.0005.png','image.0006.png','image.0007.png','image.0008.png','image.0009.png']
import glob
i = glob.glob(os.path.join(dir,"image.0*.png"))

However, since you are going to be both collecting the matching names and iterating on the files anyway fnmatch() can be a better(?) fit for processes such as these:

import os
dir = 'paziente_2019' #my folder
import fnmatch
for file in os.listdir(dir):
    if fnmatch.fnmatch(file, 'image.*.png'):
        old_file = os.path.join(dir, file)
        #print(old_file)
        new_nom = file.split("image.")[1].strip("0") #leading zero removal based on https://stackoverflow.com/a/13142375/8508004
        new_file = os.path.join(dir, new_nom)
        #print(new_file)
2 Likes