Select to view content in your preferred language

Help with arcpy.Exists()

886
2
06-19-2014 08:00 AM
ChrisBrannin
Regular Contributor
Hello

I would like to use arcpy.Exists() to handle existing featureclasses in a file-GDB but am having a problem with the argument. The problem (for me) stems from splitting a single featureclass into 2 new layers into 2 separate geodatasets. ex. Layer1 becomes Layer1_A and layer1_B from attributes A and B and are placed into datasets A and B respectively. This is all good, but when I run the script again, the files would need to be either over written (arcpy.env.overwriteOutput = True) or ignored. So I turned to arcpy.Exists(). But since the select_analysis is running twice and adding '_A' and '_B' I am not sure how to handle the argument. Any help or suggestions are greatly appreciated!

Sample code:
for filename in filenames:
        if "TOD" in [f.name for f in arcpy.ListFields(os.path.join(dirpath, filename))]:
            if not arcpy.Exists(#Something to handle A and B):
                arcpy.Select_analysis(os.path.join(dirpath, filename), r"C:/testGDB.gdb/Layer_A/" + filename + "_A", '"TOD" = \'A\'')                
                arcpy.Select_analysis(os.path.join(dirpath, filename), r"C:/testGDB.gdb/Layer_B/" + filename + "_B", '"TOD" = \'B\'')
            else:
                print "Already exists"
Tags (2)
0 Kudos
2 Replies
markdenil
Frequent Contributor
for filename in filenames:
    if "TOD" in [f.name for f in arcpy.ListFields(os.path.join(dirpath, filename))]:
        for letter in ['A', 'B']:
            newFC = r"C:/testGDB.gdb/Layer_%s/%s_%s" % (letter, filename, letter)
            if arcpy.Exists(newFC):
                arcpy.Delete_management(newFC)

            arcpy.Select_analysis(os.path.join(dirpath, filename), newFC, '"TOD" = \'%s\'' % (letter))
0 Kudos
ChrisBrannin
Regular Contributor
for filename in filenames:
    if "TOD" in [f.name for f in arcpy.ListFields(os.path.join(dirpath, filename))]:
        for letter in ['A', 'B']:
            newFC = r"C:/testGDB.gdb/Layer_%s/%s_%s" % (letter, filename, letter)
            if arcpy.Exists(newFC):
                arcpy.Delete_management(newFC)

            arcpy.Select_analysis(os.path.join(dirpath, filename), newFC, '"TOD" = \'%s\'' % (letter))


Thank you for the reply. I get error: 000210. Which is strange as there is no lock, the path is right and the file does not currently exist.
0 Kudos