I need to rename a list of feature classes (each one has only one feature) based on attribute data, i face an error says ExecuteError: ERROR 000840: The value is not a Data Element. for last line in this code... please advice

6157
21
03-12-2015 08:06 AM
Mohamed_AbdelhamidHassan
New Contributor

import arcpy

from arcpy import env

# Set the workspace for ListFeatureClasses

env.workspace = r"C:\Users\mohamed.abdelhamid\Desktop\Python\test\rename_test.gdb"

fcs = arcpy.ListFeatureClasses()

# Get the subdistric name from attribute

for itmes in fcs:

    field1  = "DIS_ARB_NA"

    field2 = "SDIS_ARB_N"

    cursor = arcpy.SearchCursor(itmes)

    for row in cursor:

        disname = row.getValue(field1)

        subname = row.getValue(field2)

        arcpy.Rename_management(in_data=itmes,out_data=disname + "_" + subname,data_type="FeatureClass")

0 Kudos
21 Replies
Mohamed_AbdelhamidHassan
New Contributor

i face the same error within the last line in the code!!!!!

Traceback (most recent call last):

  File "C:\Python27\ArcGIS10.2\Lib\site-packages\Pythonwin\pywin\framework\scriptutils.py", line 326, in RunScript

    exec codeObject in __main__.__dict__

  File "C:\Users\mohamed.abdelhamid\Desktop\Python\test\renaming_3.py", line 21, in <module>

    arcpy.Rename_management(fc, str(dname + "_" + sname), "FeatureClass")

  File "C:\Program Files\ArcGIS\Desktop10.2\arcpy\arcpy\management.py", line 4048, in Rename

    raise e

ExecuteError: ERROR 999999: Error executing function.

The dataset name is invalid.

Failed to execute (Rename).

>>>

0 Kudos
AhmedEl-Sisi
Occasional Contributor III

One optimization is to use arcpy.ValidateTableName to avoid any invalid feature class name as some characters like dash is not allowed.

import arcpy

from arcpy import env

env.workspace = r"C:\Users\mohamed.abdelhamid\Desktop\Python\test\Egy_SubDistricts_rename.gdb"

fcs = arcpy.ListFeatureClasses()

fields = ["DIS_ARB_NA","SDIS_ARB_N"]

for fc in fcs:

    with arcpy.da.SearchCursor(fc,fields) as cursor:

        for row in cursor:
            fullname= row[0] + row[1]
           
        del row, cursor

    print fullname
    outfc = arcpy.ValidateTableName(fullname,env.workspace)
    arcpy.Rename_management(fc, outfc, "FeatureClass")
0 Kudos