Tuesday, June 12, 2012

Introducing env.scratchFolder (ArcGIS 10.1)


The scratch folder is a new environmental variable that is used for the development of both published and non-published scripts.  It will point to a known folder that exists, which is very nice because at 10.0, env.scratchWorkspace or env.workspace could always be set to None or even worse set to a file geodatabase.
From the online help:
"The Scratch Folder is the location of a folder you can use to write file-based data, such as shapefiles, text files, and layer files. It is a read-only environment that is managed by ArcGIS. 
The Scratch Folder environment complements the scratch workspace environment. Its primary purpose is for use by scripts and models as geoprocessing services, with the added focus of pointing only to a known folder. When ArcGIS for Server executes a geoprocessing task, the Scratch Folder is always available to write output to. This environment is also useful in authoring scripts and models for use on the desktop. Writing output to the Scratch Folder will make your tool portable, because this location will always be available or created at execution time."
 Some important notes about env.scratchFolder:

  1. It will always exists
  2. It's read only property
  3. It will always be folder even if your scratch workspace is set to a file geodatabase
  4. If there is no Scratch Workspace set in your map document, or you're working in an environment where the Scratch Workspace has not been explicitly set, the Scratch Folder defaults to the current user's temporary files directory. This directory is typically at C:\Users\\AppData\Local\Temp on Windows 7 or C:\Documents and Settings\\Localsystem\Temp on Windows XP.

Example:
>>> import arcpy
>>> from arcpy import env
>>> env.scratchWorkspace = r"c:\temp\myfgdb.gdb"
>>> print env.scratchFolder
u'c:\\temp'


Enjoy