Creating a geodatabase and setting it to env.workspace

3778
12
Jump to solution
03-23-2017 06:55 AM
EricEagle
Occasional Contributor III

Any reason why this wouldn't be working?

import arcpy
import os

basedir = 'C:\\Temp'
gdb_name = 'TestGDB'
gdb = os.path.join(basedir, gdb_name)

if arcpy.Exists(gdb):
    arcpy.env.workspace = gdb
else:
    gdb = arcpy.CreateFileGDB_management(basedir, gdb_name)
    arcpy.env.workspace = gdb‍‍‍‍‍‍‍‍‍‍‍‍

I must not be understanding how .Exists() resolves, for if the gdb already exists, it gives me:

arcgisscripting.ExecuteError: ERROR 000258: Output C:\Temp\TestGDB.gdb already exists

Failed to execute (CreateFileGDB)

If the gdb does not exist, it gives me:

RuntimeError: Object: Error in accessing environment <workspace>

0 Kudos
1 Solution

Accepted Solutions
IanMurray
Frequent Contributor

Can you check to make sure it creates the GDB?  I think its not creating it at all.

Make line 11 just:

arcpy.CreateFileGDB_management(basedir, gdb_name)

Right now instead of creating the actual gdb, you are setting a variable equal to it, which I'm not sure what that would do but it likely isn't creating it.

View solution in original post

12 Replies
JoshuaBixby
MVP Esteemed Contributor

When creating the file geodatabase path for setting a workspace, you need to include the ".gdb" on the end, otherwise you are setting the workspace to a folder.

IanMurray
Frequent Contributor

You are missing the file extension  in your gdb variable.  gdb_name needs the extension on it before you join the path.

EricEagle
Occasional Contributor III

Thanks Ian/Joshua,

When I include the .gdb extension (gdb_name = 'TestGDB.gdb') and the geodatabase exists, it completes.  When the geodatabase does not exist, it creates the TestGDB.gdb object, but still gives me the same runtime error.

0 Kudos
IanMurray
Frequent Contributor

Can you check to make sure it creates the GDB?  I think its not creating it at all.

Make line 11 just:

arcpy.CreateFileGDB_management(basedir, gdb_name)

Right now instead of creating the actual gdb, you are setting a variable equal to it, which I'm not sure what that would do but it likely isn't creating it.

EricEagle
Occasional Contributor III

Thanks Ian.  It felt like I should be able to dump the method into a variable in one step like other methods to save a line of code, but regardless, it works now, thank you.

0 Kudos
IanMurray
Frequent Contributor

Hm it does create the GDB when you set it equal to a variable. I guess the issue with your script is the fact that arcpy.env.workspace is looking for a workspace as an input, whereas you were setting it equal to geoprocessing operation that creates the workspace. 

0 Kudos
JonathanQuinn
Esri Notable Contributor

Just to provide more insight, if you print the gdb variable, you'll get "<basedir>\<gdb_name>".  The OS separator between the out file path and FGDB name isn't escaped properly, as it's set to a backslash, which is why you see that runtime error when trying to use the gdb variable as is.  In one line, this worked:

gdb = str(arcpy.CreateFileGDB_management(outDir,outName)).replace("\\","/")

You can replace the single backslash, (escaped with a double backslash), with a forward slash.

JoshuaBixby
MVP Esteemed Contributor

I am using ArcGIS 10.5, and Create File GDB returns properly escaped results:

>>> var = arcpy.CreateFileGDB_management(r'C:\tmp','test')
>>> var
<Result 'C:\\tmp\\test.gdb'>‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍
>>> arcpy.Exists(var)
True
>>> 

The double backslashes are perfectly valid, they don't have to be forward slashes.

0 Kudos
JonathanQuinn
Esri Notable Contributor

If you call on the variable without printing, it'll be correct as a Result object.  If you print the variable you'll see the backslash is not escaped:

>>> gdb = arcpy.CreateFileGDB_management("C:/Temp","blah.gdb")
>>> gdb
<Result 'C:/Temp\\blah.gdb'>
>>> print(gdb)
C:/Temp\blah.gdb
>>> arcpy.Exists(gdb)
True
>>> arcpy.env.workspace = gdb
Runtime error 
Traceback (most recent call last):
 File "<string>", line 1, in <module>
 File "c:\program files (x86)\arcgis\desktop10.5\arcpy\arcpy\geoprocessing\_base.py", line 541, in set_
 self[env] = val
 File "c:\program files (x86)\arcgis\desktop10.5\arcpy\arcpy\geoprocessing\_base.py", line 601, in __setitem__
 ret_ = setattr(self._gp, item, value)
RuntimeError: Object: Error in accessing environment <workspace>
>>> arcpy.env.workspace = str(gdb).replace("\\","/")
>>> arcpy.env.workspace
u'C:/Temp/blah.gdb'

There seems to be different implementations of arcpy.Exists and arcpy.env.workspace, as the latter seems to look at the result as a string.