os.getcwd()but that returns the default path for the MXD in which the tool is running, which usually isn't in the same location as the script tool(s). And, the
arcpy.env.workspaceproperty isn't an option for my scripts.
Solved! Go to Solution.
import inspect, os scriptName = inspect.getfile( inspect.currentframe() ) scriptFolder = os.path.split(scriptName)[0]
Hello,
I have some custom ArcGIS toolboxes containing script tools (v10.0 and v10.1) written in Python. Is there a way in Python to get the folder path of the script tool that is being executed?
import sys import os print sys.argv[0] print os.path.dirname(sys.argv[0])
import inspect, os scriptName = inspect.getfile( inspect.currentframe() ) scriptFolder = os.path.split(scriptName)[0]
os.path.split(inspect.getfile(inspect.currentframe()))[0]
# file test.py import sys import inspect def x(): print "inspect: ",repr(inspect.getfile(inspect.currentframe())) print "sys.argv[0]: ", repr(sys.argv[0]) x()
D:\Users\cprice>C:\Python27\ArcGIS10.1\python.exe test.py inspect: 'test.py' sys.argv[0]: 'test.py'
D:\Users\cprice>C:\Python27\ArcGIS10.1\python.exe
Python 2.7.2 (default, Jun 12 2011, 15:08:59) [MSC v.1500 32 bit (Intel)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> import test
inspect: 'test.py'
sys.argv[0]: ''
>>> test.x()
inspect: 'test.py'
sys.argv[0]: ''
>>> import test >>> test.__file__ 'test.py'
>>> import sys
>>> import inspect
>>> def x():
... print "inspect: ",repr(inspect.getfile(inspect.currentframe()))
... print "sys.argv[0]: ", repr(sys.argv[0])
...
>>> x()
inspect: '<stdin>'
sys.argv[0]: ''
>>>
>>> import sys >>> sys.version '2.7.2 (default, Jun 12 2011, 15:08:59) [MSC v.1500 32 bit (Intel)]' >>> sys.executable 'C:\\Python27\\ArcGIS10.1\\python.exe'
sys.version '2.7.2 (default, Jun 12 2011, 15:08:59) [MSC v.1500 32 bit (Intel)]' >>> sys.executable 'C:\\ArcGIS\\Desktop10.1\\bin\\ArcMap.exe'
def x():
import sys
import inspect
f = "inspect: %s" + chr(10) + "sys.argv[0]: %s" + chr(10) + \
"sys.version: %s" + chr(10) + "sys.executable: %s"
return f % \
(repr(inspect.getfile(inspect.currentframe())),
repr(sys.argv[0]),sys.version,sys.executable)
--
Executing (Calculate Value): CalculateValue x() "def x():\n import sys\n import inspect\n f = "inspect: %s" + chr(10) + "sys.argv[0]: %s" + chr(10) + "sys.version: %s" + chr(10) + "sys.executable: %s" \n return f % \\n (repr(inspect.getfile(inspect.currentframe())), repr(sys.argv[0]),sys.version,sys.executable)\n" Variant
Start Time: Wed Feb 06 10:37:05 2013
Value = inspect: '<string>'
sys.argv[0]: ''
sys.version: 2.7.2 (default, Jun 12 2011, 15:08:59) [MSC v.1500 32 bit (Intel)]
sys.executable: C:\ArcGIS\Desktop10.1\bin\ArcMap.exe
Succeeded at Wed Feb 06 10:37:05 2013 (Elapsed Time: 0.00 seconds)