Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

os.path.dirname(__file__) returns empty

Tags:

python

People also ask

What does OS path dirname (__ FILE __) return?

Return Type: This method returns a string value which represents the directory name from the specified path.

What does __ FILE __ mean in Python?

__file__ is a variable that contains the path to the module that is currently being imported. Python creates a __file__ variable for itself when it is about to import a module.

What is OS path Abspath?

os.path. abspath (path) Return a normalized absolutized version of the pathname path. On most platforms, this is equivalent to calling the function normpath() as follows: normpath(join(os.getcwd(), path)) . Changed in version 3.6: Accepts a path-like object.

What is OS path join?

os. path. join combines path names into one complete path. This means that you can merge multiple parts of a path into one, instead of hard-coding every path name manually.


Because os.path.abspath = os.path.dirname + os.path.basename does not hold. we rather have

os.path.dirname(filename) + os.path.basename(filename) == filename

Both dirname() and basename() only split the passed filename into components without taking into account the current directory. If you want to also consider the current directory, you have to do so explicitly.

To get the dirname of the absolute path, use

os.path.dirname(os.path.abspath(__file__))

can be used also like that:

dirname(dirname(abspath(__file__)))

import os.path

dirname = os.path.dirname(__file__) or '.'

os.path.split(os.path.realpath(__file__))[0]

os.path.realpath(__file__)return the abspath of the current script; os.path.split(abspath)[0] return the current dir