'How to fix missing filenames in a folder of images?

I have a folder comprising .jpg images with filenames name1.jpg, name2.jpg, etc. I wish to change their filenames to 0.jpg, 1.jpg, 2.jpg, etc. The following is my code

root_path = '/path/to/images'
for root, dirs, files in os.walk(root_path):
    for i,f in enumerate(files):
        absname = os.path.join(root, f)
        newname = os.path.join(root, str(i)+'.jpg') 
        os.rename(absname, newname)

Interestingly, some of the filenames never appear. For instance, 6.jpg is always absent. Sometimes, 9.jpg goes missing. Is there a way to resolve this issue?



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source