[Tutor] Configuaration files and paths?

Martin Walsh mwalsh at mwalsh.org
Fri Aug 7 03:46:46 CEST 2009


Allen Fowler wrote:
> 
> 
> 
>> 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...


Yeah, good catch ... looks great, and thanks for sharing your mod.

Marty


More information about the Tutor mailing list