If/Else Create File Geodatabase statement ArcPy

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..." 


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.

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

Category: arcpy Time: 2016-07-28 Views: 0

Related post

iOS development

Android development

Python development

JAVA development

Development language

PHP development

Ruby development


Front-end development


development tools

Open Platform

Javascript development

.NET development

cloud computing


Copyright (C) avrocks.com, All Rights Reserved.

processed in 0.223 (s). 12 q(s)