Make a loop that will take the same name pairs from two folders?

纵然是瞬间 提交于 2020-01-06 04:51:12

问题


One main folder that has one folder named old and another called new

The old has some folders. The new has a few of these folders with same names and nothing more.

I want to delete the folders of the old that are not present in the new first and then: make a loop that will take each file -same name -pair, and put it in the the following line:

arcpy.Append_management(["shpfromonefolder.shp", "shpfromsecondfolder.shp"],"NO_TEST")

for example: land.shp from one folder with land.shp from the other folder so it will be:

arcpy.Append_management(["land.shp", "land.shp"],"NO_TEST")

回答1:


This will delete folders in old_path if they exist do not in new_path:

import os
import shutil

old_path = r"old file path"
new_path = r"old file path"

for folder in os.listdir(old_path):
    if folder not in os.listdir(new_path):
        shutil.rmtree(os.path.join(old_path, folder))

This will find the matching shape files and pass them to arcpy.Append_management():

import os
import arcpy

for dir_path, dir_names, file_names in arcpy.da.Walk(workspace=new_path, datatype="FeatureClass"):
    for filename in file_names:
        new_file_path = os.path.join(dir_path, filename)
        folder = os.path.basename(os.path.dirname(new_file_path))
        old_file_path = os.path.join(old_path, folder, filename)

        if os.path.exists(old_file_path):
            arcpy.Append_management(inputs=[new_file_path], target=old_file_path, schema_type="NO_TEST")


来源:https://stackoverflow.com/questions/51784840/make-a-loop-that-will-take-the-same-name-pairs-from-two-folders

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!