Best way to choose a random file from a directory

If you want directories included, Yuval A's answer. Otherwise:

import os, random

random.choice([x for x in os.listdir("C:\\") if os.path.isfile(os.path.join("C:\\", x))])

import os, random
random.choice(os.listdir("C:\\")) #change dir name to whatever

Regarding your edited question: first, I assume you know the risks of using a dircache, as well as the fact that it is deprecated since 2.6, and removed in 3.0.

Second of all, I don't see where any race condition exists here. Your dircache object is basically immutable (after directory listing is cached, it is never read again), so no harm in concurrent reads from it.

Other than that, I do not understand why you see any problem with this solution. It is fine.