Skip to content

Instantly share code, notes, and snippets.

@ojii
Created July 31, 2011 17:43
Show Gist options
  • Save ojii/1117009 to your computer and use it in GitHub Desktop.
Save ojii/1117009 to your computer and use it in GitHub Desktop.
import os
from fnmatch import fnmatch
import random
def get_random_file(folder, *extensions):
"""
get_random_file('/foo/bar', '*.jpg')
"""
files = [os.path.join(folder, fname) for fname in os.listdir(folder)]
pictures = [fpath for fpath in files if any([fnmatch(fpath, ext) for ext in extensions])]
return random.choice(pictures)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment