4

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?

4

1 回答 1

6

使用glob查找它:

import os
import glob

filename = glob.glob(os.path.join(folder, name + '.*'))[0]

或使用发电机:

filename = next(glob.iglob(os.path.join(folder, name + '.*')))
于 2013-11-06T22:58:28.393 回答