I have a problem that feels easy, but I cannot come up with a satisfying solution.
I have a file structure with a directory containing a very large number of files. The file names are just their index with an unknown extension. For example, the 10th file is "10.pdf" and the 42th file is "42.png". There can be many different extensions.
I need to access the i-th file from python, given index i but not knowing the extension. This will happen a lot, so I should be able to do it efficiently.
Here are the partial solutions I could think about:
I can glob the pattern
f"{i}.*"However, I think glob will check every file in the directory? This will be very slow for a large number of files.I can save and preload the full name in a
dict, in a JSON file like{..., 10: "10.pdf", ...}This works, but I have to load and keep track of another heavy object. This feels wrong somehow...If I have a list of all allowed extensions, I can just test all possibilities. This feels weird and unnecessary, but that's my best guess for now.
What do you think ? Is one of those proposal the correct way to do it ?