I'm looking for a way to randomly select a file from a tree of directories in a manner such that any individual file has exactly the same probability of being chosen as all other files. For example in the following tree of files, each file should have a 25% chance of being chosen:
My interim solution which I'm using while I code the rest of the app is to have a function like so:
def random_file(dir):
file = os.path.join(dir, random.choice(os.listdir(dir)));
if os.path.isdir(file):
return random_file(file)
else:
return file
However this obviously biases the results depending on where they are in the tree and how many siblings are along side them in their directory so they end up with the following probabilities of being selected:
The context for the function is in a background rotation app I'm writing, so the ability to filter out unwanted file extensions from being in the results would be a bonus (although I could simply force that by choosing again if it's not the file type I want... that gets messy if there's an abundance of files of the "wrong" type, though).
On first run you select to add it to Windows Explorer and find that option available when you right-click in a folder in the file browser. There you find listed the new select random menu option. Selecting it picks a random file that is stored in the directory.
You can only select all files with the same probability if you know the total number of files in advance, so you need to create a full list first:
files = [os.path.join(path, filename)
for path, dirs, files in os.walk(dir)
for filename in files
if not filename.endswith(".bak")]
return random.choice(files)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With