1

I am trying to check to see if a scratch File Geodatabase exist. If it does not, then create one.

If it does exist I would like to delete all Feature Classes in the scratch File Geodatabase.

In the code below, it will create a scratch File Geodatabase if one does not exist. However, if one exist, it attempts to create a scratch File Geodatabase, rather than delete the existing contents.

Any thoughts?

import arcpy

arcpy.env.overwriteOutput = True
arcpy.env.scratchWorkspace = r'C:\Users\topmypath'
scratchWorkspace = arcpy.env.scratchWorkspace
scratchGDB = 'scratchGDB.gdb'

if not arcpy.Exists(scratchGDB):
    arcpy.CreateFileGDB_management(scratchWorkspace, scratchGDB)
    print "Scratch FileGDB created"
else:
    if arcpy.Exists(scratchGDB):
        for fc in arcpy.ListFeatureClasses(scratchGDB):
            arcpy.Delete_management(fc)
            print "Scratch GBD cleared..."
2
  • 3
    not answering your question: If you're on 10.1+ why not just use arcpy.env.scratchGDB and you dont have to worry about it.
    – KHibma
    Jul 28 '16 at 16:59
  • There is no need for the if on the line after the else -- it will not ever evaluate False.
    – Vince
    Jul 28 '16 at 23:23
4

If you're on 10.1 or greater, I would go with KHibma's comment. If you're pre-10.0 (btw, please put the version you're using in your tags), simply add these two lines:

import os
scratchGDB = os.path.join(scratchWorkspace, 'scratchGDB.gdb')

instead of your current scratchGDB assignment. This assigns the full path that you want, so that you don't have to keep switching back and forth between workspaces later in your code.

0

arcpy.Exists searches your current workspace. If the current workspace isn't the same as the scratch workspace, your code is looking for scratchGDB.gdb in the wrong folder. Try adding

arcpy.env.workspace = scratchWorkspace

before the if statement

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.