I have asked similar question at stackoverflow but posting here as well since it involves ArcGIS and python Add-In. Following code shows first step where i have to declare output folder as global so that later outputs can be saved in it as well. Right now I am getting an error at output folder string r'optfile/ras1'. Any help how to correctly store files in output folder and declare it as global 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'

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
                          "%f %f %f %f" % (extent.XMin, extent.YMin, extent.XMax, extent.YMax), 
                          r'optfile/ras1', "#", "#", "NONE")
  • The cross-posting for this has been answered on StackOverflow: stackoverflow.com/questions/14536176/… so this question should be closed. – blah238 Jan 27 '13 at 9:49
  • Not exactly. Use of global variables within python Add-In is still not working. I created a new function within Add-In class to declare string and required variables as global but no avail. – Ibe Jan 27 '13 at 18:24

In the line where you call arcpy.Clip_management you reference r'optfile/ras1' which is a string, you want it to be:


so that you can make use of the variable.

  • It gave an error saying "ras1 is not defined". It worked with: optfile + '/ras1' – Ibe Jan 26 '13 at 18:42
  • 1
    @Ibe, you need to learn the difference between variables and string literals. It is a very important distinction in programming. – blah238 Jan 27 '13 at 9:43
  • BTW, os.path.join(optfile,"ras1") should work presuming you want the output raster to always be named ras1. – blah238 Jan 27 '13 at 9:45

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.