How can I write a Python script that I could add to ArcToolbox which will when run will automatically create a geodatabase in the Home (current MXD file's working directory) and set it as the scratch workspace?

I am new to Python (only real coding I do is VBA) so I've just been lurking and googling for snippets of code to come up with my desired result. So far I've been stuck on accessing the Home directory; the best I could come up with is a script that will print a message which includes the current mxd filename w/the filepath. I'd need to remove the filename from the output string to even begin attempting to create a new geodatabase at the desired location. Below is my attempt at Python coding:

import arcpy


arcpy.AddMessage("The current file is {}.".format(output))

Adding to your existing code:

# import required modules
import os
# get the directory and path from the MXD location
outputfolder = os.path.dirname(output)
# Create a geodatabase in this location
outputgdb = "nameofgdb.gdb"
arcpy.CreateFileGDB_management(outputfolder, outputgdb)

EDIT: Unfortunately it looks like the ability to get ModelBuilder to recognize changes to the scratch workspace from arcpy is limited in ArcMap 10.x: Setting default gdb from ArcPy?

If it was possible to set from arcpy, this would be the syntax:

# set the new geodatabase as the scratch workspace
arcpy.env.scratchWorkspace = os.path.join(outputfolder, outputdb)

As a workaround, right click on the newly created geodatabase from the script and then set it as default geodatabase.

  • Thanks for the quick response, and especially the comment lines to help be gain a better understanding of this. I added the code to mine and ran the script from ArcToolbox. It runs without errors (after editing outputdb to outputgdb on the last line) and creates a new geodatabase (nameofgdb.gdb) in the mxd directory, but it doesn't seem to alter the scratch workspace at all. Any ideas why it's not changing the scratch workspace? Thanks. – Sandman41 Oct 28 '19 at 15:55
  • You might want to look at arcpy.env.workspace. Once the geodatabase is created you can set the workspace path using that property. – dslamb Oct 29 '19 at 13:42
  • Are you sure you want the scratch workspace vs the current workspace? See gis.stackexchange.com/questions/143123 for a good explanation of the difference between the two. Please clarify what you mean by it not changing the scratch workspace. – smiller Oct 29 '19 at 14:03
  • I think scratch workspace is what I would want set to my outputgdb. I have multiple model builder tools that automate tasks and write their outputs to %scratchworkspace% by default. Usually I just manually create a geodatabase via the Catalog window, right click on it, and select "Make Default Geodatabase", to get my desired setup. Often we have scratch and current workspace set to the same geodatabase, with the output data written to scratch being data that we intend to maintain. Currently when I run the python script, the current/scratch workspaces remain stuck on their default locations. – Sandman41 Oct 29 '19 at 15:41
  • Looks like default database is not able to be changed in arcpy in ArcMap 10.x: gis.stackexchange.com/questions/197926/…. Added this and suggested workaround to answer. – smiller Oct 29 '19 at 16:09

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.