'rename all images in sub folders in the order of the folder they are located in [closed]
Ok so I have a folder named images with a list of subfolders which contain two different images, pic1.png and pic2.png.
I am trying to move all these images into the one folder but the issue is that I first need to rename these images as I am unable to move them all into the one folder as they all have the same file name.
My sub folders are called img1 img2 img3 img4 img5 but can be anywhere up to img100 and each folder has two images pic1.png and pic2.png.
ideally I want all these images in the one folder in the order that there parent folder was found.
So pic1 and pic2 of folder img1 stay names pic1 and pic2, whilst pic1 and pic2 in folder img2 become pic3 and pic4 and so on.
The script I am using is:
import os
def main():
path = "images"
count = 1
for root, dirs, files in os.walk(path):
for i in files:
os.rename(os.path.join(root, i), os.path.join(root, "pic" + str(count) + ".png"))
count += 1
if __name__ == '__main__':
main()
But it is not renaming the images in the order of there parent folder.
as the file names were renamed in the order of folder names:
| img4 | img1 | img3 | img5 | img2 |
=------=------=------=------=-------=
| pic1 | pic3 | pic5 | pic7 | pic9 |
| pic2 | pic4 | pic6 | pic8 | pic10 |
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
