Reputation: 42329
I need to open a file for reading, I know the folder it is in, I know it exists and I know the (unique) name of it but I don't know the extension before hand.
How can I open the file for reading?
Upvotes: 5
Views: 2229
Reputation: 298106
Use glob
to find it:
import os
import glob
filename = glob.glob(os.path.join(folder, name + '.*'))[0]
Or with a generator:
filename = next(glob.iglob(os.path.join(folder, name + '.*')))
Upvotes: 7