More

Arcpy.Exists and else statement


This code will go to WATERLINES.gdb, go through the gdb to see if the fc exist (based on a wildcard query), then add the fc to the mxd as desired. My questions is if the fc doesn't exist, the "else" isn't met. That is, the warning "No Waterline data exist for the PWS." isn't added to the tool dialog. It just keeps running the rest of the code. I'm curious as to why?

Edit: I added more of the code.

# Import modules import arcpy import os # Set overwrite option from arcpy import env env.overwriteOutput = True pws = arcpy.GetParameterAsText(0) waterlines = "WATERLINE_" + pws + "_*" wl_layer = "Waterlines " + pws wl_symb = r"G:Source Water ProtectionPWS_ID_ToolWATERLINES.lyr" # Set current mxd and dataframe. mxd = arcpy.mapping.MapDocument("CURRENT") df = arcpy.mapping.ListDataFrames(mxd, "")[0] env.workspace = r"G:WATER_FACILITYWATERLINES.gdb" wlFclass = arcpy.ListFeatureClasses(waterlines) arcpy.AddMessage(wlFclass) for wl in wlFclass: wlPath = os.path.join(env.workspace,wl) if arcpy.Exists(wlPath): arcpy.AddMessage("Waterline data exist for this PWS.") arcpy.MakeFeatureLayer_management(wlPath, wl_layer) arcpy.ApplySymbologyFromLayer_management(wl_layer, wl_symb) newLayer = arcpy.mapping.Layer(wl_layer) arcpy.mapping.AddLayer(df, newLayer, "TOP") else: arcpy.AddWarning("No waterline data exist for this PWS.")

Edit: the wildcard creates a list of one fc and will only ever have one. to go from unicode to a "regular" string. then create the path for the one fc to use in the exist function. when I run the tool when a fc exist i get this messgae in the tool dialog:

Executing: waterlinestest 465 Start Time: Thu Sep 10 12:33:57 2015 Running script waterlinestest… [u'WATERLINE_465_CAW'] Waterline data exist for this PWS. Completed script waterlinestest… Succeeded at Thu Sep 10 12:34:45 2015 (Elapsed Time: 47.36 seconds)

if there is no fc: Executing: waterlinestest 492 Start Time: Thu Sep 10 12:41:26 2015 Running script waterlinestest… [] Completed script waterlinestest… Succeeded at Thu Sep 10 12:42:13 2015 (Elapsed Time: 46.85 seconds)

So yes, even though the list created has nothing in it, it recognizes that it "exists".


It seems you never get to the else statement since you cursor each feature classes in the list - hence they all exist! Instead, I would check if the feature class waterlines is in the list and then process it. If it is not in the list, then the else statement will be called.

# Add waterlines env.workspace = r"G:WATER_FACILITYWATERLINES.gdb" wlFclass = arcpy.ListFeatureClasses(waterlines) if "waterlines" in wlFclass: wlPath = os.path.join(env.workspace, "waterlines") arcpy.AddMessage("Waterline data exist for this PWS.") arcpy.MakeFeatureLayer_management(wlPath, wl_layer) arcpy.ApplySymbologyFromLayer_management(wl_layer, wl_symb) newLayer = arcpy.mapping.Layer(wl_layer) arcpy.mapping.AddLayer(df, newLayer, "TOP") else: arcpy.AddWarning("No waterline data exist for this PWS.")

Hope it helps.


This is the code what works. "I needed the len(wlFclass) == 1:" to set the condition. I'm new to This Site not sure if it is proper to answer my own question in this case, but this is what will be added to my script.

# Import modules import arcpy import os # Set overwrite option from arcpy import env env.overwriteOutput = True pws = arcpy.GetParameterAsText(0) waterlines = "WATERLINE_" + pws + "_*" wl_layer = "Waterlines " + pws wl_symb = r"G:Source Water ProtectionPWS_ID_ToolWATERLINES.lyr" # Set current mxd and dataframe. mxd = arcpy.mapping.MapDocument("CURRENT") df = arcpy.mapping.ListDataFrames(mxd, "")[0] env.workspace = r"G:WATER_FACILITYWATERLINES.gdb" wlFclass = arcpy.ListFeatureClasses(waterlines) arcpy.AddMessage(wlFclass) if len(wlFclass) == 1: for wl in wlFclass: wlPath = os.path.join(env.workspace,wl) arcpy.AddMessage(wlPath) if arcpy.Exists(wlPath): arcpy.AddMessage("Waterline data exist for this PWS.") arcpy.MakeFeatureLayer_management(wlPath, wl_layer) arcpy.ApplySymbologyFromLayer_management(wl_layer, wl_symb) newLayer = arcpy.mapping.Layer(wl_layer) arcpy.mapping.AddLayer(df, newLayer, "TOP") else: arcpy.AddWarning("No waterline data exist for this PWS.")

All the feature classes returned by the ListFeatureClass exist and the "else" part of the statement is never reached.

try this:

wlFclass = arcpy.ListFeatureClasses() for wl in wlFclass: if wl == 'waterlines': arcpy.AddMessage("Waterline data exist for this PWS.") arcpy.MakeFeatureLayer_management(wlPath, wl_layer) arcpy.ApplySymbologyFromLayer_management(wl_layer, wl_symb) newLayer = arcpy.mapping.Layer(wl_layer) arcpy.mapping.AddLayer(df, newLayer, "TOP") else: arcpy.AddWarning("No waterline data exist for this PWS.")

Based on your edit, I am unsure what exactly how you wish to handle, so instead I will point out where is the flaw in our logic (please don't take offense). I've added comment to your code…

env.workspace = r"G:WATER_FACILITYWATERLINES.gdb" wlFclass = arcpy.ListFeatureClasses(waterlines) #You created a list with a wildcard for waterlines. arcpy.AddMessage(wlFclass) for wl in wlFclass: #You then iterate each feature class within this list. wlPath = os.path.join(env.workspace,wl) #You then create a path for the current feature class, read from the list if arcpy.Exists(wlPath): #You check to see if the feature class exist, which will always be True since you just got it from a list of valid feature classes within our geodatabase. That is why you will never get to the else statement. arcpy.AddMessage("Waterline data exist for this PWS.") arcpy.MakeFeatureLayer_management(wlPath, wl_layer) arcpy.ApplySymbologyFromLayer_management(wl_layer, wl_symb) newLayer = arcpy.mapping.Layer(wl_layer) arcpy.mapping.AddLayer(df, newLayer, "TOP") else: arcpy.AddWarning("No waterline data exist for this PWS.")

Instead, I would check how many element is in your list. Something like this maybe…

env.workspace = r"G:WATER_FACILITYWATERLINES.gdb" wlFclass = arcpy.ListFeatureClasses(waterlines) arcpy.AddMessage(wlFclass) if len(wlFclass) > 0: arcpy.AddMessage("Waterline data exist for this PWS.") for wl in wlFclass: wlPath = os.path.join(env.workspace,wl) arcpy.MakeFeatureLayer_management(wlPath, wl_layer) arcpy.ApplySymbologyFromLayer_management(wl_layer, wl_symb) newLayer = arcpy.mapping.Layer(wl_layer) arcpy.mapping.AddLayer(df, newLayer, "TOP") else: arcpy.AddWarning("No waterline data exist for this PWS.")


Watch the video: 3 - Simple Script - ArcMap Scripting with Python and Arcpy (October 2021).