Best way to choose a random file from a directory

PythonFileRandom

Python Problem Overview


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

dir = 'some/directory'
filename = random.choice(dircache.listdir(dir))
path = os.path.join(dir, filename)

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

Python Solutions


Solution 1 - Python

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.

Solution 2 - Python

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

Solution 3 - Python

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
n=0
random.seed();
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/randy.py
#! /usr/bin/env python
import sys, random
random.seed()
n = 1
for line in sys.stdin:
  if random.uniform(0, n) < 1:
      rline=line
  n += 1
sys.stdout.write(rline)

$ /tmp/randy.py < /usr/share/dict/words 
chrysochlore

$ find /tmp/foo -type f | /tmp/randy.py
/tmp/foo/bar

Solution 4 - Python

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

random_file=random.choice(os.listdir("Folder_Destination"))

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("\n\nList 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
    random_file=random.choice(os.listdir(source))
    print("%d} %s"%(i+1,random_file))
    source_file="%s\%s"%(source,random_file)
    dest_file=dest
    #"shutil.move" function moves file from one directory to another
    shutil.move(source_file,dest_file)

print("\n\n"+"$"*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


Solution 5 - Python

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.

Solution 6 - Python

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.

Solution 7 - 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

Solution 8 - Python

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

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionJasonSmithView Question on Stackoverflow
Solution 1 - PythonYuval AdamView Answer on Stackoverflow
Solution 2 - PythonmavnnView Answer on Stackoverflow
Solution 3 - PythonkeithpjolleyView Answer on Stackoverflow
Solution 4 - PythonTHE_PHOENIX_777_TDWView Answer on Stackoverflow
Solution 5 - Pythonkarim79View Answer on Stackoverflow
Solution 6 - PythonMork0075View Answer on Stackoverflow
Solution 7 - PythonunwindView Answer on Stackoverflow
Solution 8 - PythonChris NormanView Answer on Stackoverflow