0

I looking for a help to understand how I can load these images from my folder in python with the order of disposition in the folder, these are numerate from 0 to 14. I ask because, the script that I found loads the images but without a precise order, and this means I can have problem to rename the images beyond the precise initial order. This order is necessary because I want rename the file using a list of precise photo names, and if these images are not in order these can finish to have a wrong name in the final folder.

the script that I found is this:

# Function to rename multiple files
def main():
    for count, filename in enumerate(os.listdir(path2)):
        dst = "Hostel" + str(count) + ".jpg"
        src = path2 + filename
        dst = path2 + dst

    # rename() function will
    # rename all the files
    os.rename(src, dst)


# Driver Code
if __name__ == '__main__':
    # Calling main() function
    main()

Also my list of images that I want to use for rename the file is the follow:

["<Camera '100_0015_0047'>", "<Camera '100_0015_0047'>", "<Camera '100_0015_0048'>",
 "<Camera '100_0015_0036'>", "<Camera '100_0015_0036'>", "<Camera '100_0015_0049'>",
 "<Camera '100_0015_0048'>", "<Camera '100_0015_0046'>", "<Camera '100_0015_0046'>",
 "<Camera '100_0015_0040'>", "<Camera '100_0015_0047'>", "<Camera '100_0015_0045'>",
 "<Camera '100_0015_0040'>", "<Camera '100_0015_0040'>", "<Camera '100_0015_0046'>"]
martineau
  • 119,623
  • 25
  • 170
  • 301
  • `listdir` and `iterdir` do not sort the results, you have to do it manually according to the logic you want (by name, by date...). If you need it by name a simple `sorted` should do. – Pietro Mar 25 '21 at 16:45
  • The files in a folder are not guaranteed to be in any particular order, which makes what you want do difficult, if not impossible. Where are you getting the names in the list of file names (and what determines their order)? – martineau Mar 25 '21 at 16:53
  • The list of name is obtained by an algorithm that crops some my photo in other smaller photo. then the algorithm created a list of these photo in order of production, and save each photo in the folder. For this mean I need to have the single file upload in order because I can't wrong the name for the photos! Do you think that you might help me? – Emanuele Papucci Mar 26 '21 at 08:00

0 Answers0