I have a directory with a large number of files (~1mil). I need to choose a random file from this directory. Since there are so many files, os.listdir naturally tak
os.listdir
try this, (here is very fast with 50K files...)
import glob import random list = glob.glob("*/*.*") print list[random.randrange(0,list.__len__())]