Get filenames using glob

Viewed 9504

I am reading several tsd files using panda and combine them to a big frame. I am using glob to iterate trough all the files in my directory and sub-directories. Every single frame gets a unique key. Now I want to create a reference table where the file name to each key is stored. But since I don't really understand glob I don't know how to get only the names of the files.

p = Path('myPath')
data = []
reference_table = {}
number_of_files = 0

for tsd_files in p.glob('**/*.tsd'):
        data.append(pd.read_csv(str(tsd_files), delim_whitespace=True, header=None))
        number_of_files = number_of_files + 1

whole_data = pd.concat(data, keys= list(range(number_of_files)))
2 Answers

Let's use Path in a pythonic way.

from pathlib import Path

p = Path('dir')
filenames = [i.stem for i in p.glob('**/*.ext')]

p.glob('**/*.ext') returns a generator object, which needed be iterated to get it values out, which done wit [i for i in ..]

i.stem means filenames with extentions.

Related