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")
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")
unfortunately not working
What is the error message?
-
Vandana
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
>>>
just a note, the feature classes within database has only one row,
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)
>>>
You can also try:
arcpy.Rename_management(fc, str(disname + "_" + subname))
#remove the feature class
-
Vandana
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.
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