Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get filenames using glob

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)))
like image 363
JuMang Avatar asked Jan 04 '23 23:01

JuMang


2 Answers

Just use os.path.basename() to get only filename from path.

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

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
    file_names.append(os.path.basename(tsd_files))

whole_data = pd.concat(data, keys= list(range(number_of_files)))
like image 129
mx0 Avatar answered Jan 06 '23 13:01

mx0


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.

like image 36
Mai Hai Avatar answered Jan 06 '23 14:01

Mai Hai