[Tutor] Configuaration files and paths?
Allen Fowler
allen.fowler at yahoo.com
Fri Aug 7 01:53:41 CEST 2009
>
> Something like this ...
>
> # lib/mypaths.py
> # --------------
>
> import os
>
> def script_path(base):
> return os.path.realpath(os.path.abspath(base))
>
> def script_dir(base):
> return os.path.dirname(script_path(base))
>
> def join_relative(base, path):
> return os.path.join(script_dir(base), path)
>
> # app.py
> # ------
>
> ...
>
> from lib.mypaths import join_relative
> print join_relative(__file__, 'config/prefs.ini')
>
> # this may work when calling from
> # other modules, I suppose (untested)
>
> import sys
> print join_relative(sys.argv[0], 'config/prefs.ini')
>
> ...
FWIW:
When using relative paths I got extra ../../ terms, so I changed join_relative() to:
def join_relative(base, path):
return os.path.normpath(os.path.join(script_dir(base), path))
Seems to work...
More information about the Tutor
mailing list