Take the 2-minute tour ×
Geographic Information Systems Stack Exchange is a question and answer site for cartographers, geographers and GIS professionals. It's 100% free, no registration required.

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")
share|improve this question
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
add comment

1 Answer

up vote 4 down vote accepted

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.

share|improve this answer
It gave an error saying "ras1 is not defined". It worked with: optfile + '/ras1' –  Ibe Jan 26 '13 at 18:42
@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
Thanks. That clears the conceptual mix. –  Ibe Jan 27 '13 at 18:19
add comment

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.