import os
print '獲取當(dāng)前目錄'
print os.getcwd()
print os.path.abspath(os.path.dirname(file))
print '獲取上級(jí)目錄'
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 '獲取上上級(jí)目錄'
print os.path.abspath(os.path.join(os.getcwd(), "../.."))
輸出結(jié)果為:
獲取當(dāng)前目錄
/workspace/demo/folder/test
/workspace/demo/folder/test
獲取上級(jí)目錄
/workspace/demo/folder
/workspace/demo/folder
/workspace/demo/folder
獲取上上級(jí)目錄
/workspace/demo