Get filenames using glob

ε祈祈猫儿з 提交于 2020-01-14 19:11:08

问题


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)))

回答1:


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)))


来源:https://stackoverflow.com/questions/44215339/get-filenames-using-glob

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!