[GIS] Using ArcPy to Rename Feature Class by Field

arcmaparcpyfeature-classfields-attributes

I made a little model and at the end I want to rename the feature class file to something based on the fields in that feature class. I want to rename that feature class by the date the data was collected (its a field in the FC) follow by an underscore then by the editor of the FC (which is also a field). The way our files are setup is the data is collected on the same date by one person. So there is no danger of having 2 editors to one file or 2 dates. I am just really new to Python.

I used it to do field calculations and have that understood fairly well. But I am trying to figure out the command to pull the field value and print it in the rename.

I have the stock python script for rename

# Name: Rename_Example2.py
# Description: Rename fileGDB feature class

# Import system modules
import arcpy
from arcpy import env

# Set workspace
env.workspace = "C:/workspace/test.gdb"

# Set local variables
in_data =  "test"
out_data = "testFC"
data_type = "FeatureClass"

# Execute Rename
arcpy.Rename_management(in_data, out_data, data_type)

How do I put the fields with the data into the "out_data"? I figure I have to collect it some how, maybe in the local variables? You don't need to write the script for me, but if you could point me to a solid source for commands and strings that would be great. I already checked out other threads and the python site.

https://docs.python.org/2/library/string.html#formatspec

It seems close to what I need but I am still not getting it.

Best Answer

You want ListFields: http://help.arcgis.com/en/arcgisdesktop/10.0/help/index.html#//000v0000001p000000

in_data = "test"
out_data = "testFC"
fields = arcpy.ListFields(in_data)
for f in fields:
    print f.name

# join prefix and desired field names with an _
new_name = '_'.join([out_data, fields[0].name, fields[1].name])

# data_type is usually optional...
data_type = "FeatureClass"

arcpy.Rename_management(in_data, new_name, data_type)