python unipath: path to current file directory (ancestor) outputs nothing -
i have been trying learn python-unipath , have been getting grips basic commands. however, have been stumped issue. so, ancestor(2) of current file. so, on python interpretter, this:
python 2.7.3 (default, jan 2 2013, 13:56:14) [gcc 4.7.2] on linux2 type "help", "copyright", "credits" or "license" more information. >>> unipath import path >>> orm_root = path("/home/foo/lump/foobar/turf/orm/unipath_try.py").ancestor(2) >>> orm_root path('/home/foo/lump/foobar/turf')
..which correct , want. now, wrap in file so:
# -*- coding: utf-8 -*- # unipath_try.py unipath import path orm_root = path(__file__).ancestor(2) print orm_root
when run using python unipath_try.py
no output! no import errors either. baffled why - stupid. appreciate help/direction on :(
use os.path.abspath(__file__)
instead of __file__
.
this because __file__
contains relative path in case.
__file__
can contain relative path or absolute 1 in different situations:
so, if aren't inside part of sys.path contains module, you'll absolute path. if inside part of sys.path contains module, you'll relative path.
if load module in current directory, , current directory isn't in sys.path, you'll absolute path.
if load module in current directory, , current directory in sys.path, you'll relative path.
Comments
Post a Comment