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
参考: