Okay, It’s about time I document this trick since I use it so often and have to keep looking it up!
I like to make life easy for my successors, so whenever I write a program that needs to access files in or below its current directory, I avoid absolute paths like the plague. I find that it’s almost always better to use a relative path, so that if the program needs to be moved someday, it will continue to run with the expected behavior, regardless of where it lives on the filesystem.
The problem is that when a program is executed, all relative paths are relative to the current working directory of the user who executed it instead of the directory that the program actually lives in. To work around this, you just need to prefix your relative paths with a variable containing the absolute location of the program itself:
PHP:
$cwd = dirname(__FILE__)
Python:
cwd = os.path.abspath(sys.path[0])
Note that the above won’t work when your program is “frozen” (e.g. compiled with py2exe).
Windows Shell:
set CWD=%~dp0
I think you might mean successors rather than predecessors. In bash/ksh/Bourne scripts, I tend to use cwd=`dirname $0`.
haha, you’re right! now let’s pretend this never happened, and that i’m not that much of an idiot. ;)