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.py dirPath = os.path.dirname(realPath) # /home/user/test dirName = os.path.basename(dirPath) # test