import arcpy, os arcpy.env.workspace = ws = r'G:\CamarilloGIS\Databases\GeoDatabases\HTE.gdb' # put all the values in here 'old' : 'new' dictionary = {GIS_CE_' : 'HTE_CodeEnforcement', 'GIS_BP_' : 'HTE_BuildingPermits', ...etc...} # this will loop thru the whole gdb and change any names that are in the dictionary variable for fc in arcpy.ListFeatureClasses(): if fc in dictionary: old = os.path.join(ws,fc) new = os.path.join(ws,dictionary[fc]) try: arcpy.Rename_management(old, new, "FeatureClass") print 'Renamed %s to %s' %(fc, dictionary[fc]) except: print 'Could not rename %s' % old
try: arcpy.Rename_management(GIS_BP_, HTE_BuildingPermits, "FeatureClass") except: print 'Could not find feature class' try: arcpy.Rename_management(GIS_CE_, HTE_CodeEnforcement, "FeatureClass") except: print 'Could not find feature class' # etc........
Interesting...What error message were you getting? I made a replica of your geodatabase in the exact same location on my G: drive and executed the same code I posted above and it worked perfectly (Maybe I had your path name wrong?). I have attached screenshots of the whole process below.[ATTACH=CONFIG]22400[/ATTACH][ATTACH=CONFIG]22401[/ATTACH][ATTACH=CONFIG]22402[/ATTACH][ATTACH=CONFIG]22403[/ATTACH]
I have a model which will pull 30 features from a database and a second model which will perform a feature class rename on these 30 different features. This is done on a daily basis. Sometimes one of the names will change or a feature won't be pulled in for some reason. The problem is that when the rename model is running if it can't find a feature it will error the entire model instead of ignoring the error and skipping to the next feature to rename. None of the processes have preconditions. Is their a way I can tell the model to ignore errors and move on if it can't find a feature? A green error instead of a red one?
def FCRename(from,to): import arcpy try: arcpy.Rename_management(from,to) return "rename %s to %s success!" % (from,to) except: return "rename %s to %s failed" % (from,to)
Still no dice and I can't figure out why. This is why I use a batch file to open up ArcCatalog and run modelbuilder, its much easier.
Caleb, I get this error. How do I define the dictionary?Traceback (most recent call last): File "C:/01Working_Directory/Python/HTErenamePythontest", line 17, in <module> if fc in dictionary:NameError: name 'dictionary' is not defined
import arcpy, os arcpy.env.workspace = ws = r'G:\CamarilloGIS\Databases\GeoDatabases\HTE.gdb' namedict = {'GIS_BP_': 'HTE_BuildingPermits','GIS_CE_': 'HTE_CodeEnforcement', 'GIS_LX_': 'HTE_LX_LocPoints_All','GIS_OL_': 'HDL_BusinessLicense_OL', 'GIS_LX_Active_': 'HTE_LX_Active_LocPoints','abc': 'HTE_ABCLicenses', 'GIS_PZ_': 'HTE_Planning_Zoning','GIS_UA_': 'HTE_UtilitiesAll_CX', 'GIS_TSI_': 'SQL_TrafficSignInv','GIS_UF_': 'HTE_Util_Fireline_CX', 'GIS_UL_': 'HTE_Util_Landscape_CX','GIS_UR_': 'HTE_Util_Refuse_Active_CX', 'GIS_US_': 'HTE_Util_Sewer_Active_CX','GIS_USSVC_': 'HTE_Util_Sewer_Service_All_CX', 'GIS_UW_': 'HTE_Util_Water_Active_CX','GIS_UWSVC_': 'HTE_Util_Water_All_CX', 'GIS_WF_': 'HTE_WorkOrders','GIS_WF_SWID_': 'HTE_StormWater_WorkOrders_SWID', 'intersections': 'HTE_Intersections','GIS_URSVC_': 'HTE_Util_Refuse_All_CX'} # Loop thru gdb for fc in arcpy.ListFeatureClasses(): if fc in namedict: old = os.path.join(ws,fc) new = os.path.join(ws,namedict[fc]) try: arcpy.Rename_management(old, new, "FeatureClass") print 'Renamed %s to %s' %(fc, namedict[fc]) except: print 'Could not rename %s' % old
Thanks for trying Caleb. I think I will just refine the model instead of trying to recode everything.
import arcpy, os arcpy.env.workspace = ws = r'G:\CamarilloGIS\Databases\GeoDatabases\HTE.gdb' namedict = {'GIS_BP_': 'HTE_BuildingPermits','GIS_CE_': 'HTE_CodeEnforcement', 'GIS_LX_': 'HTE_LX_LocPoints_All','GIS_OL_': 'HDL_BusinessLicense_OL', 'GIS_LX_Active_': 'HTE_LX_Active_LocPoints','abc': 'HTE_ABCLicenses', 'GIS_PZ_': 'HTE_Planning_Zoning','GIS_UA_': 'HTE_UtilitiesAll_CX', 'GIS_TSI_': 'SQL_TrafficSignInv','GIS_UF_': 'HTE_Util_Fireline_CX', 'GIS_UL_': 'HTE_Util_Landscape_CX','GIS_UR_': 'HTE_Util_Refuse_Active_CX', 'GIS_US_': 'HTE_Util_Sewer_Active_CX','GIS_USSVC_': 'HTE_Util_Sewer_Service_All_CX', 'GIS_UW_': 'HTE_Util_Water_Active_CX','GIS_UWSVC_': 'HTE_Util_Water_All_CX', 'GIS_WF_': 'HTE_WorkOrders','GIS_WF_SWID_': 'HTE_StormWater_WorkOrders_SWID', 'intersections': 'HTE_Intersections','GIS_URSVC_': 'HTE_Util_Refuse_All_CX',} # Loop thru gdb for fc in arcpy.ListFeatureClasses(): if fc in dictionary: old = os.path.join(ws,fc) new = os.path.join(ws,dictionary[fc]) try: arcpy.Rename_management(old, new, "FeatureClass") print 'Renamed %s to %s' %(fc, dictionary[fc]) except: print 'Could not rename %s' % old
# -*- coding: utf-8 -*- # --------------------------------------------------------------------------- # HTErename2.py # Created on: 2013-03-04 13:45:45.00000 # (generated by ArcGIS/ModelBuilder) # Description: # --------------------------------------------------------------------------- # Import arcpy module import arcpy try: # rename fc here except: arcpy.AddMessage('Could not find feature class') # Local variables: GIS_LX_Active_ = "G:\\CamarilloGIS\\Databases\\GeoDatabases\\HTE.gdb\\GIS_LX_Active_" GIS_BP_ = "G:\\CamarilloGIS\\Databases\\GeoDatabases\\HTE.gdb\\GIS_BP_" GIS_CE_ = "G:\\CamarilloGIS\\Databases\\GeoDatabases\\HTE.gdb\\GIS_CE_" GIS_LX_ = "G:\\CamarilloGIS\\Databases\\GeoDatabases\\HTE.gdb\\GIS_LX_" GIS_OL_ = "G:\\CamarilloGIS\\Databases\\GeoDatabases\\HTE.gdb\\GIS_OL_" abc = "G:\\CamarilloGIS\\Databases\\GeoDatabases\\HTE.gdb\\abc" GIS_PZ_ = "G:\\CamarilloGIS\\Databases\\GeoDatabases\\HTE.gdb\\GIS_PZ_" GIS_TSI_ = "G:\\CamarilloGIS\\Databases\\GeoDatabases\\HTE.gdb\\GIS_TSI_" GIS_UA_ = "G:\\CamarilloGIS\\Databases\\GeoDatabases\\HTE.gdb\\GIS_UA_" GIS_UF_ = "G:\\CamarilloGIS\\Databases\\GeoDatabases\\HTE.gdb\\GIS_UF_" GIS_UL_ = "G:\\CamarilloGIS\\Databases\\GeoDatabases\\HTE.gdb\\GIS_UL_" GIS_UR_ = "G:\\CamarilloGIS\\Databases\\GeoDatabases\\HTE.gdb\\GIS_UR_" GIS_URSVC_ = "G:\\CamarilloGIS\\Databases\\GeoDatabases\\HTE.gdb\\GIS_URSVC_" GIS_US_ = "G:\\CamarilloGIS\\Databases\\GeoDatabases\\HTE.gdb\\GIS_US_" GIS_USSVC_ = "G:\\CamarilloGIS\\Databases\\GeoDatabases\\HTE.gdb\\GIS_USSVC_" GIS_UW_ = "G:\\CamarilloGIS\\Databases\\GeoDatabases\\HTE.gdb\\GIS_UW_" GIS_UWSVC_ = "G:\\CamarilloGIS\\Databases\\GeoDatabases\\HTE.gdb\\GIS_UWSVC_" GIS_WF_ = "G:\\CamarilloGIS\\Databases\\GeoDatabases\\HTE.gdb\\GIS_WF_" GIS_WF_SWID2_ = "G:\\CamarilloGIS\\Databases\\GeoDatabases\\HTE.gdb\\GIS_WF_SWID2_" intersections = "G:\\CamarilloGIS\\Databases\\GeoDatabases\\HTE.gdb\\intersections" HTE_BuildingPermits = "G:\\CamarilloGIS\\Databases\\GeoDatabases\\HTE.gdb\\HTE_BuildingPermits" HTE_CodeEnforcement = "G:\\CamarilloGIS\\Databases\\GeoDatabases\\HTE.gdb\\HTE_CodeEnforcement" HTE_LX_LocPoints_All = "G:\\CamarilloGIS\\Databases\\GeoDatabases\\HTE.gdb\\HTE_LX_LocPoints_All" HDL_BusinessLicense_OL = "G:\\CamarilloGIS\\Databases\\GeoDatabases\\HTE.gdb\\HDL_BusinessLicense_OL" HTE_LX_Active_LocPoints = "G:\\CamarilloGIS\\Databases\\GeoDatabases\\HTE.gdb\\HTE_LX_Active_LocPoints" HTE_ABCLicenses = "G:\\CamarilloGIS\\Databases\\GeoDatabases\\HTE.gdb\\HTE_ABCLicenses" HTE_Planning_Zoning = "G:\\CamarilloGIS\\Databases\\GeoDatabases\\HTE.gdb\\HTE_Planning_Zoning" HTE_UtilitiesAll_CX = "G:\\CamarilloGIS\\Databases\\GeoDatabases\\HTE.gdb\\HTE_UtilitiesAll_CX" SQL_TrafficSignInv = "G:\\CamarilloGIS\\Databases\\GeoDatabases\\HTE.gdb\\SQL_TrafficSignInv" HTE_Util_Fireline_CX = "G:\\CamarilloGIS\\Databases\\GeoDatabases\\HTE.gdb\\HTE_Util_Fireline_CX" HTE_Util_Landscape_CX = "G:\\CamarilloGIS\\Databases\\GeoDatabases\\HTE.gdb\\HTE_Util_Landscape_CX" HTE_Util_Refuse_Active_CX = "G:\\CamarilloGIS\\Databases\\GeoDatabases\\HTE.gdb\\HTE_Util_Refuse_Active_CX" HTE_Util_Sewer_Active_CX = "G:\\CamarilloGIS\\Databases\\GeoDatabases\\HTE.gdb\\HTE_Util_Sewer_Active_CX" HTE_Util_Sewer_Service_All_CX = "G:\\CamarilloGIS\\Databases\\GeoDatabases\\HTE.gdb\\HTE_Util_Sewer_Service_All_CX" HTE_Util_Water_Active_CX = "G:\\CamarilloGIS\\Databases\\GeoDatabases\\HTE.gdb\\HTE_Util_Water_Active_CX" HTE_Util_Water_All_CX = "G:\\CamarilloGIS\\Databases\\GeoDatabases\\HTE.gdb\\HTE_Util_Water_All_CX" HTE_WorkOrders = "G:\\CamarilloGIS\\Databases\\GeoDatabases\\HTE.gdb\\HTE_WorkOrders" HTE_StormWater_WorkOrders_SWID2 = "G:\\CamarilloGIS\\Databases\\GeoDatabases\\HTE.gdb\\HTE_StormWater_WorkOrders_SWID2" HTE_Intersections = "G:\\CamarilloGIS\\Databases\\GeoDatabases\\HTE.gdb\\HTE_Intersections" HTE_Util_Refuse_All_CX = "G:\\CamarilloGIS\\Databases\\GeoDatabases\\HTE.gdb\\HTE_Util_Refuse_All_CX" try: # rename fc here except: arcpy.AddMessage('Could not find feature class') # Process: Rename (2) arcpy.Rename_management(GIS_BP_, HTE_BuildingPermits, "FeatureClass") # Process: Rename (3) arcpy.Rename_management(GIS_CE_, HTE_CodeEnforcement, "FeatureClass") # Process: Rename (4) arcpy.Rename_management(GIS_LX_, HTE_LX_LocPoints_All, "FeatureClass") # Process: Rename (5) arcpy.Rename_management(GIS_OL_, HDL_BusinessLicense_OL, "FeatureClass") # Process: Rename (7) arcpy.Rename_management(GIS_LX_Active_, HTE_LX_Active_LocPoints, "FeatureClass") # Process: Rename (6) arcpy.Rename_management(abc, HTE_ABCLicenses, "FeatureClass") # Process: Rename (8) arcpy.Rename_management(GIS_PZ_, HTE_Planning_Zoning, "FeatureClass") # Process: Rename (9) arcpy.Rename_management(GIS_UA_, HTE_UtilitiesAll_CX, "FeatureClass") # Process: Rename (10) arcpy.Rename_management(GIS_TSI_, SQL_TrafficSignInv, "FeatureClass") # Process: Rename (11) arcpy.Rename_management(GIS_UF_, HTE_Util_Fireline_CX, "FeatureClass") # Process: Rename (12) arcpy.Rename_management(GIS_UL_, HTE_Util_Landscape_CX, "FeatureClass") # Process: Rename (13) arcpy.Rename_management(GIS_UR_, HTE_Util_Refuse_Active_CX, "FeatureClass") # Process: Rename (14) arcpy.Rename_management(GIS_US_, HTE_Util_Sewer_Active_CX, "FeatureClass") # Process: Rename (15) arcpy.Rename_management(GIS_USSVC_, HTE_Util_Sewer_Service_All_CX, "FeatureClass") # Process: Rename (16) arcpy.Rename_management(GIS_UW_, HTE_Util_Water_Active_CX, "FeatureClass") # Process: Rename (17) arcpy.Rename_management(GIS_UWSVC_, HTE_Util_Water_All_CX, "FeatureClass") # Process: Rename (18) arcpy.Rename_management(GIS_WF_, HTE_WorkOrders, "FeatureClass") # Process: Rename (19) arcpy.Rename_management(GIS_WF_SWID2_, HTE_StormWater_WorkOrders_SWID2, "FeatureClass") # Process: Rename (20) arcpy.Rename_management(intersections, HTE_Intersections, "FeatureClass") # Process: Rename (22) arcpy.Rename_management(GIS_URSVC_, HTE_Util_Refuse_All_CX, "FeatureClass")
try: # rename fc here except: arcpy.AddMessage('Could not find feature class')
if arcpy.Exists(feature_class): # rename fc here else: arcpy.AddMessage('Could not find feature class') # or pass
Signed in members can post, follow updates, and more. New here? Register a free account.
Find useful guides, FAQs, and documents to help you navigate and make the most of Esri Community.