Help! ArcPy tool with if else statement not fully working

1016
6
06-11-2020 03:06 PM
ChristopherSanchez
New Contributor II

Hi everyone. I am creating am making a script with arcpro and need some help. The tool will delete a shapefile in my gdb if it matches the name of my input, but if it doesn't find a shapefile in the gdb it will create a shapefile with that name. the tool will online get to the point of using the delete tool, but doesnt show that it is using the create feature class tool. any help would be appreciated.

import arcpy

def CheckFcExists(ws,inFC, outFC):
   arcpy.env.workspace = ws
   if inFC == inFC:
      arcpy.Delete_management(inFC)
      arcpy.AddMessage(f"deleting...")
   else:
      arcpy.CreateFeatureclass_management(inFC,outFC)
      arcpy.AddMessage(f"creating...")



workspace = arcpy.GetParameterAsText(0)
inFeatureClass = arcpy.GetParameterAsText(1)
outFeatureClass = arcpy.GetParameterAsText(2)

if __name__ == "__main__":
   workspace = r"C:\Users\tophe\Documents\ArcGIS\Projects\Lab3_Programming\Lab3.gdb"

   CheckFcExists(workspace, inFeatureClass, outFeatureClass)

0 Kudos
6 Replies
DanPatterson
MVP Esteemed Contributor

you have a redundant definition in your inFC == inFC line, so it doesn't matter what you pass the function, it will always be true.

a = "real a"
def test(a):
    if a == a:
        print(a)
    else:
        print("nope")
        

test("not a")
not a

... sort of retired...
ChristopherSanchez
New Contributor II

I changed it from inFC==inFC to ws == inFC, but it gives me back an error when I run the tool. i also put the input feature class name without the .shp.

Thank you for your help

0 Kudos
ChristopherSanchez
New Contributor II

0 Kudos
DanPatterson
MVP Esteemed Contributor

Not exactly sure what you are trying to do, but a shapefile normally reside in a folder but you are checking it against a gdb.  Are you trying to see if a featureclass exists with the name of the shapefile (ie minus the .shp extension), 


... sort of retired...
ChristopherSanchez
New Contributor II

Yeah I'm trying to see if it exists in the gdb, if it does im deleting it, if what I input doesn't exist i'm creating it.

0 Kudos
JoshuaBixby
MVP Esteemed Contributor

As Dan pointed out, shape files can't exist in geodatabase workspaces, they exist in filesystem folder workspaces.  If you are wanting to see if a certain shape file has a corresponding feature class named after it, strip off the (.shp) on the end of the name:

>>> shp = "fghgh.shp"
>>> fc =shp[:-4]
>>> fc
'fghgh'
>>>‍‍‍‍‍‍‍‍‍‍
0 Kudos