Select to view content in your preferred language

Relative Paths in Python?

10390
3
05-04-2010 02:30 PM
JordanHoaglund
Emerging Contributor
This is probably a pretty basic question, but how do you set relative paths in Python?

The script sits in a folder called SDE.  It is run from a toolbox also located in the SDE folder.  It needs to recognize data within folders that are also located in the SDE folder (horizontal to the script in the directory tree).  Any Suggestions?  I've tried just about every combination of ../ and ./ in front of filepaths in the script to no avail.  Maybe it's not that easy??

Thanks,
-Jordan
3 Replies
JasonScheirer
Esri Alum
Unfortunately, this is quite difficult to accomplish in 9.3.1, but in 10.0 you can use the __file__ global in script tools to determine the folder a script is running in. The same __file__ global can be used in script tools in 9.3.1, but only if the script is marked to run out of process (in the properties dialog).

The recipe you'd use is:

os.path.join(os.path.dirname(__file__), "my_sde.sde")
0 Kudos
JordanHoaglund
Emerging Contributor
Thanks,

I think I found something that works pretty easily from

http://www.faqs.org/docs/diveintopython/regression_path.html


import sys, os

print 'sys.argv[0] =', sys.argv[0]            
pathname = os.path.dirname(sys.argv[0])       
print 'path =', pathname
print 'full path =', os.path.abspath(pathname)


I modified it in my script to just include;

pathname = os.path.dirname(sys.argv[0])



And then when I defined my local variables I went with;

VariableName = pathname + "/Folder/File"


-Jordan
0 Kudos
DaleHoneycutt
Deactivated User
See the ArcGIS help topic  Techniques for sharing Python scripts 

The first section "Finding data relative to the script location" shows you how to do this...
0 Kudos