Python 3.x 处理名称中共享字符串且文件夹名为Python 3的文件

Python 3.x 处理名称中共享字符串且文件夹名为Python 3的文件,python-3.x,directory,Python 3.x,Directory,我有数千个名为aoi**u seg*.shp的文件。它们存储在以下文件夹中: .\segment\u polygon\poly5numSeg\compactness40\aoi01\aoi01\u seg0.shp .\segment\u polygon\poly5numSeg\compactness40\aoi01\aoi01\u seg1.shp .\segment\u polygon\poly5numSeg\compactness40\aoi01\aoi01\u seg2.shp .\se

我有数千个名为aoi**u seg*.shp的文件。它们存储在以下文件夹中:

.\segment\u polygon\poly5numSeg\compactness40\aoi01\aoi01\u seg0.shp .\segment\u polygon\poly5numSeg\compactness40\aoi01\aoi01\u seg1.shp .\segment\u polygon\poly5numSeg\compactness40\aoi01\aoi01\u seg2.shp

.\segment\u polygon\poly5numSeg\compactness40\aoi02\aoi02\u seg0.shp .\segment\u polygon\poly5numSeg\compactness40\aoi02\aoi02\u seg1.shp .\segment\u polygon\poly5numSeg\compactness40\aoi02\aoi02\u seg2.shp

.\segment\u polygon\poly6numSeg\compactness40\aoi01\aoi01\u seg0.shp .\segment\u polygon\poly6numSeg\compactness40\aoi01\aoi01\u seg1.shp .\segment\u polygon\poly6numSeg\compactness40\aoi01\aoi01\u seg2.shp

我要处理属于同一子文件夹的所有文件,将它们放入列表中,对它们进行处理,并将唯一的输出保存到相应的文件夹中:

.\segment\u multipoly\multi5numSeg\compactness40\aoi01\aoi01\u joined.shp

.\segment\u multipoly\multi5numSeg\compactness40\aoi02\aoi02\u joined.shp

.\segment\u multipoly\multi6numSeg\compactness40\aoi01\aoi01\u joined.shp

到目前为止,我已经管理了以下内容,但它将所有文件放在同一个列表中每个aoi需要一个列表,对该数据集应用一个函数,该列表的输出将其存储在输出文件夹中。

segment_polygon = os.path.join(output, "segment_polygon\\") # input folder
segment_multipoly = os.path.join(output, "segment_multipoly\\") # output folder

# 1. get aoi directories
aoi_dir = [path for path in glob.glob(os.path.join(segment_polygon, "*/*/*"))
           if os.path.isdir(path)]

# output
#['C:\\Users\\uuuuu\\HiDrive\\PhD\\RS_GIS_PhDAnalysisDaten\\medellin_ortophoto\\segmentation_aoi\\segment_polygon\\poly5numSeg\\compactness40\\aoi01', 
#'C:\\Users\\uuuuu\\HiDrive\\PhD\\RS_GIS_PhDAnalysisDaten\\medellin_ortophoto\\segmentation_aoi\\segment_polygon\\poly5numSeg\\compactness40\\aoi02']


# list to store the shapefiles to be intersected
input_list = []

for path in aoi_dir:
    # 2. get the files
    shp_paths = glob.glob(path + os.sep + '*.shp')
    for shp_path in shp_paths:
        # 3. do things with shp_path
        full_path, seg_shp = os.path.split(shp_path)
        aoi_folder = full_path[-5:] # aoi01, aoi02, aoi03....aoi25
        if seg_shp.startswith(aoi_folder):
            input_list.append(shp_path) # here I want the list only for the files that share the same aoi name and subfolder
            ''' apply a function to the list '''
            ''' creates output called *auto_inter* '''
            # 4. create your output file path
        output_path = full_path.replace("poly", "multi")
        N_output_path = output_path.replace("gon", "polygon")
        print(f"output_path:\n {N_output_path}")
        # make sure the directories exist
        if not os.path.exists(os.path.dirname(N_output_path)):
            os.makedirs(os.path.dirname(N_output_path), exist_ok=True)
            # create output file name
            multipoly_name = aoi_folder + ".shp"
            # export output
            auto_inter.to_file(os.path.join(N_output_path, multipoly_name))
我没能解决这个问题。我在Windows10和Python 3中工作