You can get it at the following.
os.path.dirname(os.path.abspath(__file__))
Test code for operation check
get_abs_dirname.py
import os
print("__file__ : %r" % __file__)
print("os.path.dirname(__file__) : %r" % (os.path.dirname(__file__)))
print("os.path.abspath(__file__) : %r" % (os.path.abspath(__file__)))
print("os.path.dirname(os.path.abspath(__file__)): %r" % (os.path.dirname(os.path.abspath(__file__))))
Execution result
__file__ : 'get_abs_dirname.py'
os.path.dirname(__file__) : ''
os.path.abspath(__file__) : '/home/nagamee/work/get_abs_dirname.py'
os.path.dirname(os.path.abspath(__file__)): '/home/nagamee/work/'
Recommended Posts