import os
print '获取当前目录'
print os.getcwd()
print os.path.abspath(os.path.dirname(file))
print '获取上级目录'
print os.path.abspath(os.path.dirname(os.path.dirname(file)))
print os.path.abspath(os.path.dirname(os.getcwd()))
print os.path.abspath(os.path.join(os.getcwd(), ".."))
print '获取上上级目录'
print os.path.abspath(os.path.join(os.getcwd(), "../.."))
输出结果为:
获取当前目录
/workspace/demo/folder/test
/workspace/demo/folder/test
获取上级目录
/workspace/demo/folder
/workspace/demo/folder
/workspace/demo/folder
获取上上级目录
/workspace/demo