get script directory name - Python [duplicate] get script directory name - Python [duplicate] python python

get script directory name - Python [duplicate]


import osos.path.basename(os.path.dirname(os.path.realpath(__file__)))

Broken down:

currentFile = __file__  # May be 'my_script', or './my_script' or                        # '/home/user/test/my_script.py' depending on exactly how                        # the script was run/loaded.realPath = os.path.realpath(currentFile)  # /home/user/test/my_script.pydirPath = os.path.dirname(realPath)  # /home/user/testdirName = os.path.basename(dirPath) # test


Just write

import osimport os.pathprint( os.path.basename(os.getcwd()) )

Hope this helps...