P
P
Pan Propan2017-10-10 17:00:27
Python
Pan Propan, 2017-10-10 17:00:27

How to select multiple random files in a directory in PYthon?

AXIS - Windows.
Actually on a subject.
If 1 file needs to be selected, then it’s clear, something like this:

import os
from os import path
files = [f for f in os.listdir(dirToScreens) if path.isfile(f)]

but what if I need to select 5 random files?
I select files in .waw format, files in the directory at the end of the day (the moment the script is executed), about 100.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
R
RussianSuburban, 2017-10-11
@mgis

import random
...
chosen_files = random.sample(files, 5) if len(files) > 4 else files

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question