Select to view content in your preferred language

How to save output files in a folder created within python Add-In?

2107
4
01-26-2013 12:59 AM
IbraheemKhan1
Occasional Contributor
Output folder is defined during execution of python Add-In and it is required that output should be stored in the created folder. But I am getting an error pertaining to output folder string such as "r'optfile/ras1'" which is also shown below. Any help how to correctly store output file in the created folder would be appreciative.

import arcpy
import os
import pythonaddins

from datetime import datetime

now = datetime.now()
month = now.month
year = now.year

optfile = "C:/temp/"+str(year)+"_"+str(month)

class DrawRectangle(object):
    """Implementation for rectangle_addin.tool (Tool)"""
    def __init__(self):
        self.enabled = True
        self.cursor = 1
        self.shape = 'Rectangle'
        os.makedirs(optfile)        

    def onRectangle(self, rectangle_geometry):
        """Occurs when the rectangle is drawn and the mouse button is released.
        The rectangle is a extent object."""

        extent = rectangle_geometry
        arcpy.Clip_management(r'D:/test', "%f %f %f %f" %(extent.XMin, extent.YMin, extent.XMax, extent.YMax), r'optfile/ras1', "#", "#", "NONE")
        arcpy.RefreshActiveView()
Tags (2)
0 Kudos
4 Replies
curtvprice
MVP Esteemed Contributor
The expression you're looking for is to generate the path using your variable "optfile" and the os.path.join function:

optfile = "C:/temp/"+str(year)+"_"+str(month)
...
    optras = os.path.join(optfile,"ras1")
    arcpy.Clip_management(r'D:/test', 
        "%f %f %f %f" % \
        (extent.XMin, extent.YMin, extent.XMax, extent.YMax),
         optras, "#", "#", "NONE")
0 Kudos
IbraheemKhan1
Occasional Contributor
Thanks. Is it possible to declare "optfile" as global string so that outputs from other tools can be saved in the same folder?
0 Kudos
curtvprice
MVP Esteemed Contributor
Thanks. Is it possible to declare "optfile" as global string so that outputs from other tools can be saved in the same folder?


The usual way to handle this is to set the current workspace. If the current workspace has the value "C:\temp", the tools will write their output to that workspace. For example:

arcpy.Clip_management(r'D:/test', 
        "%f %f %f %f" % \
        (extent.XMin, extent.YMin, extent.XMax, extent.YMax),
         "ras1", "#", "#", "NONE")

will write the output raster to "C:\temp\ras1".

If you need to access the current setting, say for the Create Feature Class tool, you can get it as "arcpy.env.workspace".
0 Kudos
IbraheemKhan1
Occasional Contributor
arcpy.env.workspace is used to define single workspace, but in current case folders (optfile) will change with each run and therefore i would like to declare it globally inside python Add-In for use by other scripts.
Let me explain it again to clarify. Say I have four scripts. Script one creates a folder and saves the output in it. Second script do some processing and generates the output which I also would like to save in the same folder created during the execution of the first script. Likewise with rest of scripts, output goes into the same folder. How to pass on the path of folder created in first script to second script to get all files in same folder.
0 Kudos