问题
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