Bruno García
Bruno García

Reputation: 187

os.rename not changing file extension in windows

I have a simple Python 3's script that rename all files in a folder, in the shell it shows the new name applied correctly, but when I go to the folder in windows, the files does not have any extension.

i.e: In python I rename somefile to: "newname.jpg", but on windows it is only "newname", without extension. I guess this is more a Windows 10's problem than a Python's problem.

This is the code:

import os
from os import listdir

i = 0
dir = "./PHOTO GALLERY 1"

for archivo in listdir(dir):
    i=i+1
    if i<10:
        j="0"+str(i)
    else:
         j=str(i)   
    print ("Nombre original: " + archivo)
    os.rename(dir + "/" + archivo, dir + "/" + "photogallery1_" + j)
    print ("Nombre nuevo: " + "photogallery1_" + j + ".jpg")

Upvotes: 0

Views: 77

Answers (1)

J. Titus
J. Titus

Reputation: 9690

It looks like you're leaving off the file extension:

os.rename(dir + "/" + archivo, dir + "/" + "photogallery1_" + j)

should be

os.rename(dir + "/" + archivo, dir + "/" + "photogallery1_" + j + ".jpg")

like in your print statement.

Upvotes: 1

Related Questions