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

5252
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
VandanaRaghunathan
New Contributor III

Hello Mohammed,

Based on your title I assume you want to rename the feature classes in a workspace based on an attribute from the feature class? Can you try this:

import arcpy

from arcpy import env

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

fields = ["DIS_ARB_NA","SDIS_ARB_N"]

for fc in arcpy.ListFeatureClasses():

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

        disname = row[0]

        subname = row[1]

        arcpy.Rename_management(fc, str(disname + "_" + subname), "FeatureClass")

0 Kudos
Mohamed_AbdelhamidHassan
New Contributor

unfortunately not working

0 Kudos
VandanaRaghunathan
New Contributor III

What is the error message?

-

Vandana

0 Kudos
Mohamed_AbdelhamidHassan
New Contributor

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_0.py", line 9, in <module>

    disname = row[8]

NameError: name 'row' is not defined

>>>

0 Kudos
Mohamed_AbdelhamidHassan
New Contributor

just a note, the feature classes within database has only one row,

0 Kudos
Mohamed_AbdelhamidHassan
New Contributor

while i run the last proposed code from your side Vandana, I face this msg error:

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_2.py", line 21, in <module>

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

UnicodeEncodeError: 'ascii' codec can't encode characters in position 0-2: ordinal not in range(128)

>>>

0 Kudos
VandanaRaghunathan
New Contributor III

You can also try:

arcpy.Rename_management(fc, str(disname + "_" + subname))

#remove the feature class

-

Vandana

0 Kudos
JosephKnieriem
New Contributor

The cursor puts a lock on your feature class.  You probably just need to populate a variable with the desired name to store it, delete the cursor, then rename the feature class.

0 Kudos
VandanaRaghunathan
New Contributor III

Hi Joseph,

Using a With statement in arcpy.da.SearchCursor will guarantee close and release of database locks and reset iteration.

http://desktop.arcgis.com/en/desktop/latest/analyze/arcpy-data-access/searchcursor-class.htm

-

Vandana

https://community.esri.com/message/467705?et=watches.email.thread#467705

0 Kudos