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

 


免责声明!

本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系本站邮箱yoyou2525@163.com删除。



 
粤ICP备18138465号  © 2018-2025 CODEPRJ.COM