Select to view content in your preferred language

FeatureClassToFeatureClass Loop With List for Output Names

493
7
Jump to solution
10-03-2023 10:31 AM
coolflippers
New Contributor III

 

ShapenameList00 = ["Cover88","Cover91"]
print(ShapenameList00)
['Cover88', 'Cover91']
print(Data)
C:\Users\jasherer\.0\PythonProject\Data
print([Data])
['C:\\Users\\jasherer\\.0\\PythonProject\\Data']
print(LandCover)
C:\Users\jasherer\.0\PythonProject\gdb0YS.gdb\LandCover
print([LandCover])
['C:\\Users\\jasherer\\.0\\PythonProject\\gdb0YS.gdb\\LandCover']

 

The following (and other versions without changing the out_name parameter) hit "RuntimeError: Object: Error in executing tool" when running from the Python window in ArcGIS Pro 3.1.0:

 

arcpy.env.workspace = Data
arcpy.ListFeatureClasses()
['landcover_1988.shp', 'landcover_1991.shp', ...]
ShapefileList00 = ["landcover_1988.shp","landcover_1991.shp"]
for EachShp in ShapefileList00:
    arcpy.conversion.FeatureClassToFeatureClass(
                                    in_features = EachShp,
                                    out_path = LandCover,
                                    out_name = ShapenameList00
                                    )
ShapefileList00 = [os.path.join(Data,"landcover_1988.shp"),os.path.join(Data,"landcover_1991.shp")]
print(ShapefileList00)
['C:\\Users\\jasherer\\.0\\PythonProject\\Data\\landcover_1988.shp', 'C:\\Users\\jasherer\\.0\\PythonProject\\Data\\landcover_1991.shp']
for EachShp in ShapefileList00:
    arcpy.conversion.FeatureClassToFeatureClass(
                                    in_features = EachShp,
                                    out_path = LandCover,
                                    out_name = ShapenameList00
                                    )​

 

0 Kudos
1 Solution

Accepted Solutions
AlfredBaldenweck
MVP Regular Contributor

Ohhhhhh. Sorry, I did misread that completely.

Okay, so what you really want here is a dictionary, where each item in the dictionary has a corresponding value, which can be anything-- a string, a list, even a whole other dictionary.

import os
newNameList00 = ["Cover88","Cover91"]

ShapefileList00 = [os.path.join(Data,"landcover_1988.shp"),os.path.join(Data,"landcover_1991.shp")]
#Zip up the two lists, create dictionary from it.
# Lists must be of equal length.
nameDict = dict(zip(ShapfileList00, newNameList00))
'''
nameDict = {'C:\\Users\\jasherer\\.0\\PythonProject\\Data\\landcover_1988.shp':"Cover88", 
'C:\\Users\\jasherer\\.0\\PythonProject\\Data\\landcover_1991.shp':"Cover91"}
'''
for EachShp in ShapefileList00:
    arcpy.conversion.FeatureClassToFeatureClass(
                                    in_features = EachShp,
                                    out_path = LandCover,
                                    out_name = nameDict[EachShp] #Look up the original shapefile in the dictionary, return the new name
                                    )

 

View solution in original post

7 Replies
AlfredBaldenweck
MVP Regular Contributor

I think it's because you're trying to name the output as the full list.

C:\\Users\\jasherer\\.0\\PythonProject\\Data\\['C:\\Users\\jasherer\\.0\\PythonProject\\Data\\landcover_1988.shp', 'C:\\Users\\jasherer\\.0\\PythonProject\\Data\\landcover_1991.shp']

 try:

ShapefileList00 = [os.path.join(Data,"landcover_1988.shp"),os.path.join(Data,"landcover_1991.shp")]
print(ShapefileList00)
['C:\\Users\\jasherer\\.0\\PythonProject\\Data\\landcover_1988.shp', 'C:\\Users\\jasherer\\.0\\PythonProject\\Data\\landcover_1991.shp']
for EachShp in ShapefileList00:
    arcpy.conversion.FeatureClassToFeatureClass(
                                    in_features = EachShp,
                                    out_path = LandCover,
                                    out_name = os.path.basename(EachShp)
                                    )

 

This will grab the actual name (landcover_1988) and stick in the output.

I'm assuming the LandCover variable is a directory path.

coolflippers
New Contributor III

I don't know what exactly constitutes a directory path, but the LandCover variable is printed in the first code block, and it points to a full path from C:.

Do I need to loop in the out_name parameter? Using the parent loop variable (EachShp) would grab the same name as from the in_features list (ShapefileList00), as you mentioned. For each item in ShapefileList00, I'd want out_name to become the corresponding item in ShapenameList00, assuming index # in ShapefileList00 corresponds with index # in ShapenameList00.

0 Kudos
AlfredBaldenweck
MVP Regular Contributor

Oh completely missed that. You should be fine; I'm assuming that's a feature dataset.

To answer your second question, yes, what I posted takes the file name (not including extension) and sticks it in as the output name. You'll have to import the os module (import os) (or you could get the filename manually but Ew) for it to work, but it should yield the results.

 

import os

ShapefileList00 = [os.path.join(Data,"landcover_1988.shp"),os.path.join(Data,"landcover_1991.shp")]
print(ShapefileList00)
['C:\\Users\\jasherer\\.0\\PythonProject\\Data\\landcover_1988.shp', 'C:\\Users\\jasherer\\.0\\PythonProject\\Data\\landcover_1991.shp']
for EachShp in ShapefileList00:
    arcpy.conversion.FeatureClassToFeatureClass(
                                    in_features = EachShp,
                                    out_path = LandCover,
                                    out_name = os.path.basename(EachShp)
                                    )
#Should yield:
#C:\\Users\\jasherer\\.0\PythonProject\\gdb0YS.gdb\\LandCover\\landcover_1988
#C:\\Users\\jasherer\\.0\PythonProject\\gdb0YS.gdb\\LandCover\\landcover_1991

 

 

coolflippers
New Contributor III

In your fix, file names for out_name are the same as in_features. I have two lists, albeit confusingly similar, which might have led you to see one list. The first list, ShapefileList00, are the shapefiles to go into the LandCover feature dataset. The second list, ShapenameList00, are the names to go with each item in the first list. I am able to run this loop with FeatureClassToGeodatabase, except without stating desired output names, because FeatureClassToGeodatabase, most of the time, just assigns the basename from the input to the output. In the case of FeatureClassToFeatureClass in my original post, I am trying to assign new output names from a list.

0 Kudos
AlfredBaldenweck
MVP Regular Contributor

Ohhhhhh. Sorry, I did misread that completely.

Okay, so what you really want here is a dictionary, where each item in the dictionary has a corresponding value, which can be anything-- a string, a list, even a whole other dictionary.

import os
newNameList00 = ["Cover88","Cover91"]

ShapefileList00 = [os.path.join(Data,"landcover_1988.shp"),os.path.join(Data,"landcover_1991.shp")]
#Zip up the two lists, create dictionary from it.
# Lists must be of equal length.
nameDict = dict(zip(ShapfileList00, newNameList00))
'''
nameDict = {'C:\\Users\\jasherer\\.0\\PythonProject\\Data\\landcover_1988.shp':"Cover88", 
'C:\\Users\\jasherer\\.0\\PythonProject\\Data\\landcover_1991.shp':"Cover91"}
'''
for EachShp in ShapefileList00:
    arcpy.conversion.FeatureClassToFeatureClass(
                                    in_features = EachShp,
                                    out_path = LandCover,
                                    out_name = nameDict[EachShp] #Look up the original shapefile in the dictionary, return the new name
                                    )

 

coolflippers
New Contributor III

Thank you. I haven't used a dictionary in my own code before, and this was elucidative. Much appreciated.

0 Kudos
coolflippers
New Contributor III

This works without paths in the lists if you set the environment:

ShapenameList00 = ["landcover_1988.shp","landcover_1991.shp"]
newShpnameList00 = ["Cover88","Cover91"]
nameDict00 = dict(zip(ShapenameList00,newShpnameList00)
arcpy.env.workspace = Data
for EachShp in ShapenameList00:
    arcpy.conversion.FeatureClassToFeatureClass(
                                    in_features = EachShp,
                                    out_path = Landcover,
                                    out_name = nameDict00[EachShp]
                                    )