It seems to me then you have at least four problems:
- Trying to traverse to one of only 20 "special" folders. Your wording of the problem makes it ambiguous as to whether you mean 20 specific folders whose names you know, or 20 random folders. I'm going to assume the former.
- Finding any and all files in these folders that are images
- For a given folder from step #1, convert ONE of these images to a .jpg file.
- Move this converted file to a brand new folder.
For #1, granted, you could use os.walk()
or glob
to search a given directory and all its subdirectories for the files that you want. But that's going to return everything, which means you'd then have to whittle it down. Instead, if you already know the 20 folders, then it may make sense to start with a list of those, and then apply os.walk()
on each of those individually to get their respective contents.
For #2, note that os.path.splitext(filename)[-1]
returns the file extension (including the '.') of the file filename
. So as you're traversing the directories and finding files, you can use this to check to see if the extension looks like the type of file you want to covert to .jpg (whatever that may be: .bmp, .png, whatever).
For #3, you can use Python's PIL
module to convert a given file. For more information on how to do that, see this previous question.
Then #4 is just a matter of using a function like os.mkdir()
to create the directory, and shutil.move()
to move it.