Best way to choose a random file from a directory

What is the best way to choose a random file from a directory in Python?

Edit: Here is what I am doing:

import os
import random
import dircache

filename = random.choice(dircache.listdir(dir))
path = os.path.join(dir, filename)

Is this particularly bad, or is there a particularly better way?

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.

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))])

The problem with most of the solutions given is you load all your input into memory, which can become a problem for large inputs/hierarchies. Here’s a solution adapted from The Perl Cookbook by Tom Christiansen and Nat Torkington. To get a random file anywhere beneath a directory:

#! /usr/bin/env python
import os, random
for root, dirs, files in os.walk('/tmp/foo'):
  for name in files:
    n += 1
    if random.uniform(0, n) < 1:
        rfile=os.path.join(root, name)
print rfile

Generalizing a bit makes a handy script:

$ cat /tmp/
#! /usr/bin/env python
import sys, random
n = 1
for line in sys.stdin:
  if random.uniform(0, n) < 1:
  n += 1

$ /tmp/ < /usr/share/dict/words 

$ find /tmp/foo -type f | /tmp/

The simplest solution is to make use of os.listdir & random.choice methods


Let’s take a look at it step by step :-

1} os.listdir method returns the list containing the name of
entries (files) in the path specified.

2} This list is then passed as a parameter to random.choice method
which returns a random file name from the list.

3} The file name is stored in random_file variable.

Considering a real time application

Here’s a sample python code which will move random files from one directory to another

import os, random, shutil

#Prompting user to enter number of files to select randomly along with directory
source=input("Enter the Source Directory : ")
dest=input("Enter the Destination Directory : ")
no_of_files=int(input("Enter The Number of Files To Select : "))

print("%"*25+"{ Details Of Transfer }"+"%"*25)
print("nnList of Files Moved to %s :-"%(dest))

#Using for loop to randomly choose multiple files
for i in range(no_of_files):
    #Variable random_file stores the name of the random file chosen
    print("%d} %s"%(i+1,random_file))
    #"shutil.move" function moves file from one directory to another

print("nn"+"$"*33+"[ Files Moved Successfully ]"+"$"*33)

You can check out the whole project on github
Random File Picker

For addition reference about os.listdir & random.choice method you can refer to tutorialspoint learn python

os.listdir :- Python listdir() method

random.choice :- Python choice() method

Language agnostic solution:

1) Get the total no. of files in specified directory.

2) Pick a random number from 0 to [total no. of files – 1].

3) Get the list of filenames as a suitably indexed collection or such.

4) Pick the nth element, where n is the random number.

Independant from the language used, you can read all references to the files in a directory into a datastructure like an array (something like ‘listFiles’), get the length of the array. calculate a random number in the range of ‘0’ to ‘arrayLength-1’ and access the file at the certain index. This should work, not only in python.

If you don’t know before hand what files are there, you will need to get a list, then just pick a random index in the list.

Here’s one attempt:

import os
import random

def getRandomFile(path):
  Returns a random filename, chosen among the files of the given path.
  files = os.listdir(path)
  index = random.randrange(0, len(files))
  return files[index]

EDIT: The question now mentions a fear of a “race condition”, which I can only assume is the typical problem of files being added/removed while you are in the process of trying to pick a random file.

I don’t believe there is a way around that, other than keeping in mind that any I/O operation is inherently “unsafe”, i.e. it can fail. So, the algorithm to open a randomly chosen file in a given directory should:

  • Actually open() the file selected, and handle a failure, since the file might no longer be there
  • Probably limit itself to a set number of tries, so it doesn’t die if the directory is empty or if none of the files are readable

Python 3 has the pathlib module, which can be used to reason about files and directories in a more object oriented fashion:

from random import choice
from pathlib import Path

path: Path = Path()
# The Path.iterdir method returns a generator, so we must convert it to a list
# before passing it to random.choice, which expects an iterable.
random_path = choice(list(path.iterdir()))

Leave a Comment