【问题标题】:match subfolder name with file name and output folder python将子文件夹名称与文件名和输出文件夹 python 匹配
【发布时间】:2020-10-22 15:36:50
【问题描述】:

我有以下文件夹文件结构:

  • mainfolder_segment_polygon
    • folder_poly5numSeg
      • subfolder_compactness40
        • 子文件夹_aoi1
          • file_aoi1_seg0.shp
          • file_aoi1_seg1.shp
        • 子文件夹_aoi2
          • file_aoi2_seg0.shp
          • file_aoi2_seg1.shp
    • folder_poly6numSeg
      • subfolder_compactness40
        • 子文件夹_aoi1
          • file_aoi1_seg0.shp
          • file_aoi1_seg1.shp
        • 子文件夹_aoi2
          • file_aoi2_seg0.shp
          • file_aoi2_seg1.shp

我希望能够从同一个文件夹 (segment_polygon) 加载所有文件,对其应用函数,然后导出到具有相同结构的另一组文件夹 (segment_multipoly)。

  • 来自r".\segmentation_aoi\segment_polygon\poly5numSeg\compactness40\aoi1"的文件应该一起处理并导出到r".\segmentation_aoi\segment_multipoly\multi5numSeg\compactness40\aoi1"

  • 来自r".\segmentation_aoi\segment_polygon\poly6numSeg\compactness40\aoi2"的文件应该一起处理并导出到r".\segmentation_aoi\segment_multipoly\multi6numSeg\compactness40\aoi2"

等等……

名称“主文件夹”、“文件夹”、“子文件夹”、“文件”只是为了表明名称属于哪个级别,但它们不是文件夹标签的一部分。

input_path = os.path.join(src, "segment_polygon\\")
output_path = os.path.join(src, "segment_multipoly\\")

root = Path(input_path)

for maindir, subdirs, shpfiles in os.walk(input_path):
    for shp in shpfiles:
        aoi_root, shp_ext = shp.split("_")
        for file in root.glob("*/*/*/*.shp"):
            part_path = Path(file).parts
            folder_numSeg_name = part_path[9] #here I get the subfolder "poly5numSeg", "poly6numSeg", etc
            folder_aoi_name = part_path[11] #here I get the subfolder "aoi1", "aoi2", etc...
            aoiprep_seg = part_path[12] # here I get the name of the file "aoi1_seg0.shp", aoi1_seg1.shp", etc
            if aoi_root == folder_aoi_name:
                '''apply a function to shp'''
                shp.to_file(os.path.join(output_path, folder_numSeg_name, "compactness40\\", folder_aoi_name, shp)

我有点迷茫。 在 Windows 10、Python 3 中工作。感谢您的所有帮助。

脚本更新

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

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

# 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) # creates the new list with shapefiles that start with the same aoiX value
        auto_inter = gpd.GeoDataFrame.from_file(input_list[0]) #process shp
        for i in range(len(input_list)-1):
            mp = gpd.GeoDataFrame.from_file(input_list[i+1]) # process shp
            auto_inter = gpd.overlay(auto_inter, mp, how='intersection') #process shp
        print(f"shp included in the list:\n {input_list}")
            # 4. create your output file path
        print(full_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
            auto_inter.to_file(os.path.join(N_output_path, multipoly_name)) #export shp

合并了来自 ygorg 的更改。但是,它需要所有 shapefile 进行交集。我只想要 aoi1 文件用于交集并保存在 aoi1 文件夹中。然后,aoi2 shapefiles 并保存在 aoi2 文件夹中,以此类推。这还不行。

【问题讨论】:

    标签: python directory


    【解决方案1】:

    混合os.walkglob 似乎很混乱。如果要处理每个 aoiX 文件夹。尝试首先列出所有这些目录,然后列出每个目录中的 .shp 文件,然后应用该函数,最后创建您的 output_path 并写入它。

    在处理文件时,最好分解您需要的内容,以免不堪重负。

    # 1. get aoi directories
    aoi_dir = [path for path in glob.glob('segment_polygon/*/*/*')
               if os.path.isdir(path)]
    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
            # 4. create your output file path
            output_path = shp_path.replace('segment_polygon', 'segment_multipoly')
            # make sure the directories exist
            os.makedirs(os.path.dirname(output_path), exist_ok=True)
            # write in output file
    

    并且始终在不处理或编写任何内容的情况下进行试运行,并打印路径,以便您确定去哪里!

    【讨论】:

    • 谢谢!这有助于解决问题的一部分。但是,我只需要能够同时处理属于同一 aoi* 的所有 aoi*seg*.shp 文件。我对他们应用的功能是一个交集。我只需要将所有 aoi1.shp 相交并将它们导出到 aoi1 文件夹中。然后,将所有aoi2_.shp放在一起,并导出到aoi2文件夹中,以此类推。
    • 我想,是 os.makedirs,不是 os.path.makedirs
    【解决方案2】:

    我设法解决了这个问题。感谢 ygorg 的输入。它让我走上了正确的道路。

    # Create a list of the subfolders of segment_polygon
    poly_dir = [path for path in glob.glob(os.path.join(segment_polygon, "*/*/*"))
           if os.path.isdir(path)]
    
    for aoi_poly in poly_dir:
    
        # define input folder
        input_subfolder = aoi_poly.split("segment_polygon\\")[1] # splits the path at "...\\" and keeps the tail (position:1)
        #print(f"input folder: {input_subfolder}")
    
        #define export folder
        export_subfolder = input_subfolder.replace("poly", "multi")
        export_folder = os.path.join(segment_multipoly, export_subfolder)
        #print(f"output folder: {export_folder}")
    
        # define name output shapefile
        numseg, compactness, aoi = [int(s) for s in re.findall(r'\d+', aoi_poly)] #extract only the integers from the "poly" path
        name_output = "aoi" + str(aoi)+ "_" + "numSeg"+ str(numseg) + "_c" + str(compactness) + ".shp" # str() is used to concatenate integers as part of the string
        #print(f"shapefile label: {name_output}")
    
        full_outputpath = os.path.join(export_folder, name_output)
        #print(f"full output path: {full_outputpath}")
    
        # intersect and merge all single polygons
        input_list = list(filter(lambda mpoly: mpoly.endswith('.shp'), os.listdir(aoi_poly)))
    
         ###### apply my function here ######
    
        # export
        filetoexport.to_file(full_outputpath)
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-04-30
      • 1970-01-01
      • 1970-01-01
      • 2018-07-28
      • 2017-04-16
      相关资源
      最近更新 更多