Select to view content in your preferred language

Finding script location... dynamically

606
2
02-06-2012 07:03 AM
C__JustinMayers
New Contributor
Should be a simple question... but those can be the hardest. I have created a script that uses ESRI's arcpy site package and it functions properly in a stand-alone environment. I am now trying to integrate this script into a toolbox to be run within an open ArcMap session and I am having trouble. In integrating this script I am using the tool structure that ESRI suggests... http://goo.gl/eesrV . The script I have created has information in the 'ToolData' folder that I need to access to run the script.

So the question is how do I locate the script's directory dynamically when running it in an open ArcMap session?  In my stand-alone script I use 'os.getcwd()'; this works because it returns the proper location of the script and I then know the structure around it. Whereas when incorporated as a script in a tool box and run within an ArcMap session 'os.getcwd()' returns the directory of the map document while the toolbox, script, and tool data are located where ever the user installed them and my script can't find this structure. Thanks for your help.
Tags (2)
0 Kudos
2 Replies
JasonScheirer
Esri Alum
Use
os.path.dirname(sys.argv[0])
or
os.path.dirname(__file__)
to get the directory
0 Kudos
DanPatterson_Retired
MVP Emeritus
0 Kudos