Most Efficient/fastest Way To Get A Single File From A Directory
What is the most efficient and fastest way to get a single file from a directory using Python? More details on my specific problem: I have a directory containing a lot of pregenera
Solution 1:
Don't have a lot of pregenerated files in 1 directory. Divide them over subdirectories if more than 'n' files in the directory.
Solution 2:
when creating the files add the name of the newest file to a list stored in a text file. When you want to read/process/delete a file:
- Open the text file
- Set filename to the name on the top of the list.
- Delete the name from the top of the list
- Close the text file
- Process filename.
Solution 3:
Just use random.choice()
on the os.listdir()
result:
import random
import os
randomfilename = random.choice(os.listdir(path_to_directory))
os.listdir()
returns results in the ordering given by the OS. Using random filenames does not change that ordering, only adding items to or removing items from the directory can influence that ordering.
If your fear that you'll have too many files, do not use a single directory. Instead, set up a tree of directories with pre-generated names, pick one of those at random, then pick a file from there.
Post a Comment for "Most Efficient/fastest Way To Get A Single File From A Directory"