maptastik
12/6/2018 - 6:59 PM

List shapefiles in a directory

Function that takes a directory as a parameter and returns a list of shapefiles. Although this doesn't explicitly involve arcpy, this function could be used for automating an action on all shapefiles in a directory using arcpy. That said, it could be used for doing lots of other things with the returned shapefiles!

import os
def shps_in_dir(search_directory, full_path = True):
    shp_list = []
    for file in os.listdir(search_directory):
        if file.endswith('.shp'):
            shp_list.append(file)
    if len(shp_list) > 0 and full_path:
        shp_list = list(map(lambda x: os.path.join(search_directory,x), shp_list))
        
    return shp_list