How to Properly Use env.scratchGDB to Gaurantee Write Access

681
4
04-29-2013 06:37 AM
MarcCusumano
Occasional Contributor
In scripting business processes it is coming up again and again where I need an intermediate workspace to write data. I am trying to copy a table to the scratch workspace but it is placing the table into a dbf in the folder where the scratchGDB is located. Here is what I have:

import arcpy, arceditor, logging, datetime, os, smtplib
from arcpy import env
arcpy.env.overwriteOutput = True
InputPrefix = "Database Connections\\Sun130 - 5151.sde\\"
tblImplsblRd = InputPrefix + "GIS_INTERFACE.SAP_IMPLAUSIBLE_READS"
lclImplsblRd = env.scratchGDB + "SAP_IMPLAUSIBLE_READS"

def copyRows(src, trgt, cfg):
    try:
        arcpy.CopyRows_management(src, trgt, cfg)
        logging.info(": Copied %s to %s" %(src, trgt))

    except Exception as e:
        print e
        logging.error(": %s" %(e))
        msgTxtErr = msgTxtErr + "\n" + "* " + str(e)
        errValue = 1

copyRows(tblImplsblRd, lclImplsblRd, "")

print "done"


When I run this from a script exported from ModelBuilder it places the table into the temporary gdb:

# -*- coding: utf-8 -*-
# ---------------------------------------------------------------------------
# temp.py
# Created on: 2013-04-29 10:29:12.00000
#   (generated by ArcGIS/ModelBuilder)
# Description: 
# ---------------------------------------------------------------------------

# Import arcpy module
import arcpy


# Local variables:
GIS_INTERFACE_SAP_IMPLAUSIBLE_READS = "Database Connections\\Sun130 - 5151.sde\\GIS_INTERFACE.SAP_IMPLAUSIBLE_READS"
SAP_IMPLAUSIBLE_READS = "C:\\Users\\TDMC0\\AppData\\Local\\Temp\\scratch.gdb\\SAP_IMPLAUSIBLE_READS"

# Process: Copy Rows
arcpy.CopyRows_management(GIS_INTERFACE_SAP_IMPLAUSIBLE_READS, SAP_IMPLAUSIBLE_READS, "")



However if the path to the scratch GDB is hard-coded I will lose the ability to have a gauranteed space to write data. Would appreciate help getting this working correctly.
Tags (2)
0 Kudos
4 Replies
ChrisSnyder
Regular Contributor III
Looks like you just need to include a path seperator (i.e. "\\")

So instead of:

lclImplsblRd = env.scratchGDB + "SAP_IMPLAUSIBLE_READS"

use one of these:

lclImplsblRd = env.scratchGDB + "\\SAP_IMPLAUSIBLE_READS"
lclImplsblRd = os.path.join(env.scratchGDB, "SAP_IMPLAUSIBLE_READS")
lclImplsblRd = env.scratchGDB + os.path.sep + "SAP_IMPLAUSIBLE_READS"
0 Kudos
ChrisSnyder
Regular Contributor III
BTW: You might find the "in_memory" workspace another (maybe better) option for what you need

http://help.arcgis.com/en/arcgisdesktop/10.0/help/index.html#//002w0000005s000000
0 Kudos
MarcCusumano
Occasional Contributor
Thanks I added the "\\" but am still getting errors due to schema locks. I was under the impression that could not happen (hence the whole point of the scratchGDB environment). If there is something else I am doing wrong please let me know.

I will implement the in-memory workspace after testing is complete but in the development stage I need to see the intermediate tables structures.
0 Kudos
ChrisSnyder
Regular Contributor III
schema locks


Surefire way to clear schema locks:
If you are or ever have looked at the contents of the scratch workspace with your current ArcMap session, close ArcMap and reopen it.

If you are or ever have looked at the contents of the scratch workspace with your current ArcCatalog session, browse to another workspace/directory and hit F5.

If you are or ever have looked at the contents of the scratch workspace with Python via arcpy, close your Python IDE, and reopen it.
0 Kudos