[GIS] How to test for ‘if arcpy.exists’ with RemoveFeatureClassFromTopology stand-alone script

arcgis-10.2arcpyerror-999999topologyversioning

I have some dataset topologies that I want to loop through, but I need to test for the assignment of feature classes so the scripts don't fail. (Using Win 7, ArcGIS 10.2, Python 2.7.5 and a file GDB)

My research hasn't turned up too much but using the 10.2 "Remove Feature Class From Topology (Data Management)", using ArcCatalog I can remove an existing fc from the topo set. If however I run the script 'stand alone' and the fc isn't currently assigned to the topology, the script fails. (Error shown below script)

I read this post (ArcMap remove feature-class from feature-dataset) as well but I don't want to delete the fc, I only want to remove it from the topo if it exists as I need it again later.

Am I going down the wrong path?

Using ArcCatalog, this succeeds if the fc exists;
topo = "C:/Data/Terrain_36/TOPO_EXPORT_2015_09_09_1_2.gdb/ZONES_1_2/TOPO_1_2"
fc = "RL_10"
arcpy.RemoveFeatureClassFromTopology_management(topo, fc)

# Running the script below as 'stand alone', fails if the fc isnt currently assigned;
# import modules
import arcpy, sys, os, datetime, traceback, string, time
from os import sep, listdir
from arcpy import env  as env

# This scripts name
script_name = os.path.basename(sys.argv[0])
# Create file prefix for unique file naming
prefix = datetime.datetime.now().strftime('%Y-%m-%d %H%M')
# Create file suffix for avoiding filenames staring with numbers
suffix = datetime.datetime.now().strftime('%Y-%m-%d %H%M')

# Set variables
Top_Dir = "C:/Data/Terrain_36"
td = Top_Dir
print "Top_Dir set to: '" +td+ "'" + '\n'
# Out_Dir = td+sep+ 'Exports'
# print "Out_Dir set to: '" +Out_Dir+ "'" + '\n'
Log_Dir = td+sep+ 'zz_logs'
print "Log_Dir set to: '" +Log_Dir+ "'" + '\n'
GDB_Nam = 'TOPO_EXPORT_2015_09_09_1_2.gdb'
print "GDB_Nam set to: '" +GDB_Nam+ "'" + '\n'
GDB_Home = os.path.join(td, GDB_Nam)
print  "GDB_Home set to: '" +GDB_Home+ "'" + '\n'

# Set the workspace environment
env.workspace = os.path.join(Top_Dir, GDB_Nam)
ws = env.workspace
print "Env.workspace set to: '" +ws+ "'" '\n'
topo = "C:/Data/Terrain_36/TOPO_EXPORT_2015_09_09_1_2.gdb/ZONES_1_2/TOPO_1_2"
fc = "RL_10"
if arcpy.Exists (fc):
    arcpy.RemoveFeatureClassFromTopology_management(topo, fc)

# My error messages
Log: 2015-09-14 11:48:32.722000
Begin process: Topo_script.py
 Process started at 2015-09-14 11:48:32.722000
 PYTHON ERRORS:
 Traceback info:
   File "C:\Data\Terrain_36\Data_quality\_Scripts\Topo_script.py", line 62, in <module> arcpy.RemoveFeatureClassFromTopology_management(topo, fc)
 ArcPy ERRORS:
 ERROR 999999: Error executing function.
 Cannot register as versioned on this database.
 Cannot register as versioned on this database.
 Failed to execute (RemoveFeatureClassFromTopology).

Best Answer

Why not use arcpy.Describe on the topology itself and see if the featureclass is in the featureClassNames attribute?

Something simple like:

if fc in arcpy.Describe(topo).featureClassNames:
    arcpy.RemoveFeatureClassFromTopology_management(topo, fc)
else:
    print "{0} wasn't in the topology...".format(fc)
Related Question