Been looking at all the Python libraries I can find and still have not been able to figure this out. I have script that creates a layer but sometimes the end result of that later returns no records. How do I tell Python to stop the process if this layer is empty?
Thanks
Solved! Go to Solution.
I found another example that I am trying as well which worked for half of what I am doing so I made need to do 2 seperate scripts to make it work the way I want it to work.
# Name: DeleteFeatures_Example2.py
# Description: Delete features from a feature class based on an expression
# Import system modules
import arcpy
from arcpy import env
# Set environment settings
env.workspace = "C:/data/airport.gdb"
# Set local variables
inFeatures = "parcels"
outFeatures = "C:/output/output.gdb/new_parcels"
tempLayer = "parcelsLayer"
expression = arcpy.AddFieldDelimiters(tempLayer, "PARCEL_ID") + " = 'Cemetery'"
try:
# Execute CopyFeatures to make a new copy of the feature class
arcpy.CopyFeatures_management(inFeatures, outFeatures)
# Execute MakeFeatureLayer
arcpy.MakeFeatureLayer_management(outFeatures, tempLayer)
# Execute SelectLayerByAttribute to determine which features to delete
arcpy.SelectLayerByAttribute_management(tempLayer, "NEW_SELECTION",
expression)
# Execute GetCount and if some features have been selected, then
# execute DeleteFeatures to remove the selected features.
if int(arcpy.GetCount_management(tempLayer).getOutput(0)) > 0:
arcpy.DeleteFeatures_management(tempLayer)
except Exception as e:
# If an error occurred, print line number and error message
import traceback
import sys
tb = sys.exc_info()[2]
Hi Stephen.
In ArcGIS 9.3 (python 2.6) I had this code:
gp = arcgisscripting.create(9.3) # You can change it for import arcpy in v10
intable = gp.GetParameter(0) # this is the layer. If you have it already in a variable, it's done.
# open cursor
rows = gp.searchcursor(intable) # ...or arcpy instead of gp,
# maybe the correct sintax of this function in v10 is SearchCursor, read the help reference below
row = rows.next()
And now, check the row variable and decide if continue or not.
Good luck!
I hope it helps
Help reference of this function:
Ayuda de ArcGIS 10.1
Luis
Thanks Luis but unfortunately that didn't solve my issue.
Stephen,
I am not a Python guy but in VB and C I generally use a Record Count. If the count is zero then exit the process else carry on.
These Links my help you to do this in Python
Thanks Ted but that shows me how to count rows but doesn't tell me how to make the script stop running if the count is zero.
Let me be a little more clear on what I am trying to do. I am running queries that seek out differences in two features and creating a point file with the differences from one to the other and adding it to a mxd all using Python script and I have this first part working great but many times the layer that is created has no records because there are no differences when the comparison is run and I do not want the script to add empty layers to my map. Hope that clarifys a bit.
....?....
Some ways of exiting a python script albeit not gracefully...
sys.exit()
quit()
os.exit()
I am not a python programmer, but some pseudo code would go along these lines. Ok, if at the point you are going to write the point file (differences)... It sound like you need to break up your python script into discrete functions then there would be no need to stop?
....
def CheckForDifferences(args to do you differences):
....
...
... get your record count
return CountOfRecords
def CreateFile( your args):
...
return
def AddToMDX( your args):
...
return
# Main part of your script if there are no differences then no file will be created no need to stop or exit prematurely.
Difference = CheckForDifferences(args);
if Difference > 0:
CreateFile(your args):
AddToMDX(your args);
I found another example that I am trying as well which worked for half of what I am doing so I made need to do 2 seperate scripts to make it work the way I want it to work.
# Name: DeleteFeatures_Example2.py
# Description: Delete features from a feature class based on an expression
# Import system modules
import arcpy
from arcpy import env
# Set environment settings
env.workspace = "C:/data/airport.gdb"
# Set local variables
inFeatures = "parcels"
outFeatures = "C:/output/output.gdb/new_parcels"
tempLayer = "parcelsLayer"
expression = arcpy.AddFieldDelimiters(tempLayer, "PARCEL_ID") + " = 'Cemetery'"
try:
# Execute CopyFeatures to make a new copy of the feature class
arcpy.CopyFeatures_management(inFeatures, outFeatures)
# Execute MakeFeatureLayer
arcpy.MakeFeatureLayer_management(outFeatures, tempLayer)
# Execute SelectLayerByAttribute to determine which features to delete
arcpy.SelectLayerByAttribute_management(tempLayer, "NEW_SELECTION",
expression)
# Execute GetCount and if some features have been selected, then
# execute DeleteFeatures to remove the selected features.
if int(arcpy.GetCount_management(tempLayer).getOutput(0)) > 0:
arcpy.DeleteFeatures_management(tempLayer)
except Exception as e:
# If an error occurred, print line number and error message
import traceback
import sys
tb = sys.exc_info()[2]