python 获取文件本身的绝对路径

 

tester.py:

import os
print (os.path.dirname(__file__))
print (os.path.abspath(__file__))
print (os.path.abspath(os.path.dirname(__file__)))
print (os.path.dirname(os.path.abspath(__file__)))

输出

bogon:Desktop macname$ python3 tester.py 

/Users/macname/Desktop/tester.py
/Users/macname/Desktop
/Users/macname/Desktop



bogon:api_automation macname$ python3 ../../tester.py 
../..
/Users/macname/Desktop/tester.py
/Users/macname/Desktop
/Users/macname/Desktop

 

 

参考:

https://www.cnblogs.com/royfans/p/7908009.html

 

posted @ 2019-08-22 16:48  anobscureretreat  阅读(5323)  评论(0编辑  收藏  举报