gwydion93
gwydion93

Reputation: 1923

How to find files in multilevel subdirectories

Suppose I have a directory that contains multiple subdirectories:

one_meter = r"C:\Projects\NED_1m"

Within the directory one_meter I want to find all of the files that end with '.xml' and contain the string "_meta". My problem is that some of the subdirectories have that file one level donw, while others have it 2 levels down EX:

one_meter > USGS_NED_one_meter_x19y329_LA_Jean_Lafitte_2013_IMG_2015 > USGS_NED_one_meter_x19y329_LA_Jean_Lafitte_2013_IMG_2015_meta.xml
one_meter > NY_Long_Island> USGS_NED_one_meter_x23y454_NY_LongIsland_Z18_2014_IMG_2015 > USGS_NED_one_meter_x23y454_NY_LongIsland_Z18_2014_IMG_2015_meta.xml

I want to look in my main directory (one_meter') and find all of the_meta.xmlfiles (regardless of the subdirectory) and append them to a list (one_m_lister = []`). I tried the following but it doesn't produce any results. What am I doing incorrectly?

one_m_list = []
for filename in os.listdir(one_meter):
    if filename.endswith(".xml") and "_meta" in filename:
        print(filename)
        one_m_list.append(filename)

Upvotes: 0

Views: 918

Answers (2)

Laurent LAPORTE
Laurent LAPORTE

Reputation: 22942

The answer of @JonathanDavidArndt is good but quite outdated. Since Python 3.5, you can use pathlib.Path.glob to search a pattern in any subdirectory.

For instance:

import pathlib

destination_root = r"C:\Projects\NED_1m"
pattern = "**/*_meta*.xml"

master_list = list(pathlib.Path(destination_root).glob(pattern))

Upvotes: 2

JonathanDavidArndt
JonathanDavidArndt

Reputation: 2732

The function you are looking for is os.walk

A simple and minimal working example is below. You should be able to modify this to suit your needs:

destination_root  = "C:\Projects\NED_1m"
extension_to_find = ".xml"

master_list = []
extension_to_find_len = len(extension_to_find)
for path,dir,files in os.walk(destination_root):
    for filename in files:
        # and of course, you can add extra filter criteria
        # such as "contains _meta" right in here
        if filename[-extension_to_find_len:] == extension_to_find:
            print(os.path.join(path, filename))
            master_list.append(os.path.join(path, filename))

Upvotes: 1

Related Questions