How could i get 10 random files, or not, from a directory without listing or iterating over all files. The directory has millions of files and we can't change that. Thanks !
Asked
Active
Viewed 104 times
1
-
2Welcome to Stack Overflow! It looks like you want us to write some code for you. While many users are willing to produce code for a coder in distress, they usually only help when the poster has already tried to solve the problem on their own. A good way to demonstrate this effort is to include the code you've written so far, example input (if there is any), the expected output, and the output you actually get (console output, stack traces, compiler errors - whatever is applicable). The more detail you provide, the more answers you are likely to receive. Check the [FAQ] and [ask] – Inbar Rose Nov 21 '13 at 14:00
-
1have you tried creating a list or dict of all files in a directory (using sys), and then choosing a random item from the list? That is much easier. I do not believe it is possible to select a random file w/o finding all of the files. – Vikram Saran Nov 21 '13 at 14:00
-
2Index the files, and use a rand function to grab 10 of the files. You are going to have to list them in some way, otherwise it won't be random. – Mike Gardner Nov 21 '13 at 14:01
-
1You cannot pick random items out of a directory without at least *iterating* through them all. You won't have to hold them all in memory. What platform is this on? – Martijn Pieters Nov 21 '13 at 14:02
1 Answers
1
Create a file which contains all the names of the files. Update this file, say, once a day.
In Python, read this single file into a list, pick 10 random items.

Aaron Digulla
- 321,842
- 108
- 597
- 820