I'm having some trouble figuring something out.
I have a script that validates feature classes in a Topology, exports the errors, then writes a feature class for each individual dataset and geometry type for the errors.
For example if the feature class LINE came back having point and line errors two feature classes would get written (to a gdb). LINE_pointErrors and Line_lineErrors. The resulting GDB could end up having multiple feature classes for multiple data sets (i.e. LINE_pointErrors, LINE_lineErrors, LINE2_lineErrors, LINE3_pointErrors).
What I would like to do is export all of the feature classes representing the same dataset as shapefiles to a unique folder.
So, using the examples above, there would be three folders LINE, LINE2 and LINE3 and each would contain the shapefiles pertaining to that dataset (LINE would contain LINE_pointErrors and LINE_lineErrors, LINE2 would contain LINE2_lineErrors and LINE3 would contain LINE3_pointErrors).
I'm not even sure where to start. Any help would be appreciated.
Solved! Go to Solution.
If all Feature Classes use the same naming convention, the code below should work. It finds unique FCs bases on the [0] item in a string ('LINE' will get returned from 'LINE_pointErrors_1'). Then, it creates a folder for each FC group, then exports all FCs as .shp to that folder if the names match.
import arcpy
import os
#database and workspace
db = r'path_to_database'
arcpy.env.workspace = db
#root directory to house output shapefiles
root = r'path_to_output_shp_folder'
#all Feature Classes in db
allFCs = arcpy.ListFeatureClasses()
#loop through FC and append each unique name to list
rootNames = []
for x in allFCs:
fc = x.split('_')[0] #assuming all FCs have an _ in the name
if fc not in rootNames:
rootNames.append(fc)
#loop through FC list and create directories
for r in rootNames:
output = os.path.join(root, r)
os.mkdir(output)
for i in allFCs: #if a FC is found in the being looped over dir (r), export
item = r.split('_')[0]
if item in i:
arcpy.FeatureClassToFeatureClass_conversion(i, output, i)
If all Feature Classes use the same naming convention, the code below should work. It finds unique FCs bases on the [0] item in a string ('LINE' will get returned from 'LINE_pointErrors_1'). Then, it creates a folder for each FC group, then exports all FCs as .shp to that folder if the names match.
import arcpy
import os
#database and workspace
db = r'path_to_database'
arcpy.env.workspace = db
#root directory to house output shapefiles
root = r'path_to_output_shp_folder'
#all Feature Classes in db
allFCs = arcpy.ListFeatureClasses()
#loop through FC and append each unique name to list
rootNames = []
for x in allFCs:
fc = x.split('_')[0] #assuming all FCs have an _ in the name
if fc not in rootNames:
rootNames.append(fc)
#loop through FC list and create directories
for r in rootNames:
output = os.path.join(root, r)
os.mkdir(output)
for i in allFCs: #if a FC is found in the being looped over dir (r), export
item = r.split('_')[0]
if item in i:
arcpy.FeatureClassToFeatureClass_conversion(i, output, i)
I converted this to a question (vs a discussion) since I think you are asking and looking for an answer. This way you can mark one correct, if found (and any helpful ones too) to close the thread. Questions tend to get more views and comments/answers.
Thank you very much Mitch. That worked perfectly.
You're welcome. I'm glad I could help!