Rename all files in a folder with sequencial file name

Hello All experts…good day…please help fix my code. I have list of files in a folder. All are *.docx. I want o rename all in sequence like example i have 5 files. I want to rename it to P001 to P005. Hoping for your support.

_ext = ".docx"
numnum = 1
for i,filename in enumerate(os.listdir(destination_folder)):
    if filename.endswith(_ext):
        print (filename)
        numnum+=1
        os.rename(filename, "P00" + str(numnum) + _ext)

What happens when you try using the code, and how is that different from what you want to happen?

Also: Is this a different question from the previous thread?

1 Like

Related to previous one…i am planning after copy file i will just rename it into sequence. I manage to fix it with below code.

i = 1
dict = {}
for filename in os.listdir(destination_folder):
    my_dest1 ="P00" + str(i) + ".docx"
    my_source =destination_folder + filename
    my_dest =destination_folder + my_dest1
    
    dict[my_dest1] = filename 
    
    os.rename(my_source, my_dest)
    
    i += 1

The outout is i converted to dictionary as below:

{'P001.docx': 'EuropeL200n.docx',
 'P002.docx': 'EVG.docx',
 'P003.docx': 'spindle.docx'}

Any idea how to convert this dictionary to dataframe? thanks in advance,
I want the dataframe with below result:

Code                    File
P001.docx           EuropeL200n.docx
P001.docx           EVG.docx
P001.docx           spindle.docx

Thanks