I have a master directory that contains 300 sub directories. Each of these 300 subdirectories contains even more subdirectories, which contain jpegs, pngs.
I would like a script that will crawl through main directory, and in to each of the 300 sub main directories. From there, I would like to select one image randomly and have these images put in jpg/png format into a new folder.
I know I can randomly select images from one folder...but I'm asking how to best to navigate through all preceding subdirectories?
Thanks to a post already on this forum I am able to select and printout 20 random images:
import os
import random
files_list = []
for root, dirs, files in os.walk("/path/to/master/directory"):
for file in files:
if file.endswith(".jpeg") or file.endswith(".png"):
files_list.append(os.path.join(root, file))
# choose randomally 20 files
print(random.sample(files_list, 20))
But I need to figure out how to make sure each of the 300 subdirectories are crawled individually, returning one jpg or png which are then all placed in a new folder...so a new folder with 300 randomly selected images.