Thursday 27 September 2018

arcpy - Using FeatureClassToFeatureClass from list to concatenated list?


Have many feature classes in geodatabases and eventually want to run more complex processes on them. Having trouble with the basics - can anyone tell me what I am doing wrong with the following? If I use "print fcscopy" I get the list of output file names I want.. but the featureclass to featureclass gives following error.



#Code
import arcpy

arcpy.env.workspace = r"D:\_StuData\GIS\Projects\PersonalProjects\Product\test.gdb"
fcs = arcpy.ListFeatureClasses()
copy = 'copy'
fcscopy = [x + copy for x in fcs]
outpath = "D:\\_StuData\\GIS\\Projects\\PersonalProjects\\Product\\test.gdb"

arcpy.FeatureClassToFeatureClass_conversion(fcs, outpath, fcscopy)




Runtime error Traceback (most recent call last): File "", line 12, in File "c:\program files (x86)\arcgis\desktop10.3\arcpy\arcpy\conversion.py", line 1789, in FeatureClassToFeatureClass raise e RuntimeError: Object: Error in executing tool





>>> print fcs


[u'Border', u'Building', u'Concrete', u'Grass', u'Gravel', u'Rocks', u'Roof']


>>> print fcscopy



[u'Bordercopy', u'Buildingcopy', u'Concretecopy', u'Grasscopy', u'Gravelcopy', u'Rockscopy', u'Roofcopy']




Answer



arcpy.ListFeatureClasses() returns a list of the names of the feature classes. However, arcpy.FeatureClassToFeatureClass_conversion requires the path to the feature class. You could incorporate something like this:


import os, arcpy

arcpy.env.workspace = r"D:\_StuData\GIS\Projects\PersonalProjects\Product\test.gdb"

outpath = r"D:\_StuData\GIS\Projects\PersonalProjects\Product\test.gdb"


fcs = arcpy.ListFeatureClasses()

copy = '_copy'

for fc in fcs:
arcpy.FeatureClassToFeatureClass_conversion(os.path.join(arcpy.env.workspace, fc),outpath, fc+copy)

os.path.join will "join one or more path components intelligently."


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...