我一直在努力学习python-unipath,并且一直在掌握基本命令。然而,我一直被这个问题困扰。所以,我想获得当前文件的祖先(2)。所以,在Python解释器上,我做这样的事情:python unipath:当前文件目录(祖先)的路径不输出
Python 2.7.3 (default, Jan 2 2013, 13:56:14)
[GCC 4.7.2] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> from 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')
..这是正确的,正是我想要的。 现在,我在一个文件中把这个包像这样:
# -*- coding: utf-8 -*-
# unipath_try.py
from unipath import Path
ORM_ROOT = Path(__file__).ancestor(2)
print ORM_ROOT
,当我使用这个我python unipath_try.py
得到任何输出运行!没有导入错误。 我完全不知道为什么 - 可能真的很愚蠢。 希望得到任何帮助/方向这:(
非常感谢解决方案!现在接受你的答案:) – JohnJ
只是一个后续问题:在所有情况下使用'os.path.abspath(__ file __)'作为默认值而不是'__file__'会更好吗? – JohnJ
@JohnJ yup,将适用于所有情况。 – alecxe