0

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.

heroZero
  • 173
  • 3
  • 18
  • fair comment @ranifisch ...will update OP when I get a chance, for now just wondering if it's actually possible. – heroZero Nov 19 '19 at 13:50

1 Answers1

2

You can use the os.walk(there are more ways, but this is my favorite, you can find more here) function to iterate over all files in a given directory.

Read more about random.sample here

import os
import random

files_list = []
for root, dirs, files in os.walk("your/path/to/master/dir"):
    for file in files:
        if file.endswith(".jpeg") or file.endswith(".png"):
            files_list.append(os.path.join(root, file))
    
# print 20 random images
print(random.sample(files_list, 20))
Xorifelse
  • 7,878
  • 1
  • 27
  • 38
nonamer92
  • 1,887
  • 1
  • 13
  • 24