Thursday, 19 April 2018

arcpy - Selecting files with same first 5 characters in filename for merging?


I have a number of polygon feature classes in a geodatabase dataset. The file names are as below:


e.g. IHO1a_xxx-xxxx_ALB,IHO1b_xxx-xxxx_MBES_10m, IHO1b_xxx-xxxx_MBES_2m

I would like to get a python script which will select all files starting with exactly the same first 14 characters in the filename and merge them.


import os,arcpy

# Define location of .gdb

arcpy.env.workspace = "D:\Users\Documents\H-11_Documents for Reports_Paul\Test_Model_Builder\ICP_Intermediate.gdb\Deliveryxx_Clipped_Po lygons"

#Make workspace a variable
workspace = arcpy.env.workspace

List = []

for dirpath, dirnames, filenames in arcpy.da.Walk(workspace, datatype="FeatureClass", type = "Polygon"):
for filename in filenames:
if filename.startswith(filename[0:13]):

List.append(os.path.join(dirpath, filename))
print List
arcpy.Merge_management([filename,filename], 'D:\Users\cadetpn\Documents\H-11_Documents for Reports_Paul\Test_Model_Builder\Temp.gdb\Temp_Data\merge%n%')

##To keep console window open
raw_input("Press enter to exit...")


No comments:

Post a Comment

arcpy - Changing output name when exporting data driven pages to JPG?

Is there a way to save the output JPG, changing the output file name to the page name, instead of page number? I mean changing the script fo...