How to use lists in MakeFeatureLayer tool?

535
5
Jump to solution
01-25-2023 10:49 AM
Labels (3)
Davec43
Occasional Contributor

I'm using ListFeatureClasses to list all the layers in the GDB that I created and added layers to from a previous step. I then create another list and split the "\\" out and am left with a list of serials that I want. But when I add the two lists to the MakeFeatureLayer tool I get an error?

 

 

arcpy.env.workspace = r'\\FilePath\Tracks.gdb'
GDB = arcpy.env.workspace

full_fcs = []
for fc in arcpy.ListFeatureClasses():
    full_fcs.append(os.path.join(GDB,fc))
print(full_fcs)

serials = []
for i in full_fcs:
    serials.append(i.split("\\")[-1])
print(serials)
['T07JDD9C00100DP', 'T095XH6K002006B', 'T0K1CF8W3AH5FKP', 'T1WNBH3K00201QB', 'T3N3BHBA012024T', 'T3NZCJ9L0146GRK', 'T3NZSHAP00304LW']
arcpy.management.MakeFeatureLayer(full_fcs, serials)
Traceback (most recent call last):
  File "<string>", line 1, in <module>
  File "C:\Program Files\ArcGIS\Pro\Resources\ArcPy\arcpy\management.py", line 9395, in MakeFeatureLayer
    raise e
  File "C:\Program Files\ArcGIS\Pro\Resources\ArcPy\arcpy\management.py", line 9392, in MakeFeatureLayer
    retval = convertArcObjectToPythonObject(gp.MakeFeatureLayer_management(*gp_fixargs((in_features, out_layer, where_clause, workspace, field_info), True)))
  File "C:\Program Files\ArcGIS\Pro\Resources\ArcPy\arcpy\geoprocessing\_base.py", line 512, in <lambda>
    return lambda *args: val(*gp_fixargs(args, True))
RuntimeError: Object: Error in executing tool

 

 

 

 

0 Kudos
2 Solutions

Accepted Solutions
JohannesLindner
MVP Frequent Contributor

https://pro.arcgis.com/en/pro-app/latest/tool-reference/data-management/make-feature-layer.htm

The tool expects you to input a single path and a single layer name. So you have to do it in a loop:

for path, name in zip(full_fcs, serials):
    arcpy.management.MakeFeatureLayer(path, name)

Have a great day!
Johannes

View solution in original post

RhettZufelt
MVP Frequent Contributor

Since trying to make feature layer, I assume the 'serials' are the names of the feature classes in the FGDB?

Looks like you are trying to make a feature layer for all the feature classes in the database.

Since the workspace is set, no need to split them up, just interate through the listfeatures and feed to the make features:

 

GDB = arcpy.env.workspace = r'\\FilePath\Tracks.gdb'

for fc in arcpy.ListFeatureClasses():
    arcpy.MakeFeatureLayer_management(fc, f"{fc}_lyr")

 

R_

 

View solution in original post

5 Replies
JohannesLindner
MVP Frequent Contributor

https://pro.arcgis.com/en/pro-app/latest/tool-reference/data-management/make-feature-layer.htm

The tool expects you to input a single path and a single layer name. So you have to do it in a loop:

for path, name in zip(full_fcs, serials):
    arcpy.management.MakeFeatureLayer(path, name)

Have a great day!
Johannes
Davec43
Occasional Contributor

This worked thank you!

0 Kudos
RhettZufelt
MVP Frequent Contributor

Since trying to make feature layer, I assume the 'serials' are the names of the feature classes in the FGDB?

Looks like you are trying to make a feature layer for all the feature classes in the database.

Since the workspace is set, no need to split them up, just interate through the listfeatures and feed to the make features:

 

GDB = arcpy.env.workspace = r'\\FilePath\Tracks.gdb'

for fc in arcpy.ListFeatureClasses():
    arcpy.MakeFeatureLayer_management(fc, f"{fc}_lyr")

 

R_

 

Davec43
Occasional Contributor

This worked! Although the f"{fc}_lyr" added _lyr to the end of every layer it brought over. Removing the "_lyr" fixed it and gave me the output I was looking for.

0 Kudos
RhettZufelt
MVP Frequent Contributor

Glad it worked.  Pretty standard to append the _lyr to the end.  That way, when feeding the output into another tool/process, is easier to distinguish the layer from the original FC.

R_