简体   繁体   中英

How to read all the files of the same type of an arborescence in Python?

I am using the TUH EEG Seizure corpus and I would like to get easily with Python all the files with the same extension. I saw that post but I don't know if it can be applied to an overall hierarchy.

Thanks in advance

glob allow to automatically select a pattern in a single directory. os.walk is the tool to use for browsing a full hierarchy, but it has no provision for filtering file names on a specific pattern, so you have to apply the filtering by hand . You could do:

import os.path

# enter your real data here
top_folder = ...
extension = ...

# and let's browse:
for folder, sub_folders, files in os.walk(top_folder)
    for file in files:
        if file.endswith(extension):
            full_path = os.path.join(folder, file)
            # apply your processing to full_path

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM