python 获取文件目录位置

 test.py

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(), "../..")))

 

bogon:Desktop macname$ pwd
/Users/macname/Desktop
bogon:Desktop macname$ python3 test.py 
***获取当前目录***
/Users/macname/Desktop
/Users/macname/Desktop
***获取上级目录***
/Users/macname/Desktop
/Users/macname
/Users/macname
***获取上上级目录***
/Users

 

posted @ 2019-04-10 19:34  anobscureretreat  阅读(6704)  评论(0编辑  收藏  举报