os.path.dirname(__file__) returns empty
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__))
os.path.dirname returning empty string in django
The os.path.dirname(os.path.dirname(__file__))
does not make any sense. You need to call os.path.dirname(os.path.abspath(__file__))
as suggested in the duplicate question.
The result is empty because __file__
contains only the file name of the Python file.
os.path.dirname
does not resolve the location of the given file but simply strips away the file name from the given string.
os.path.dirname('../foo/bar/baz.txt')
Out[4]: '../foo/bar'
As you can see the path has not been resolved, the bar.txt
has simply been removed from the string.What you're doing is the equivalent of:
__file__ = 'baz.txt'
os.path.dirname(__file__)
Out[6]: ''
Instead you should do:os.path.dirname(os.path.abspath(__file__))
Out[10]: '/home/noxdafox/foo/bar'
os.path.join(os.path.dirname(__file__)) returns nothing
I guess with nothing you mean an empty string? This could only be the case, if __file__
was an empty string in the first place. Did you accidentally overwrite __file__
?
Difference between os.path.dirname(os.path.abspath(__file__)) and os.path.dirname(__file__)
BASE_DIR
is pointing to the parent directory of PROJECT_ROOT
. You can re-write the two definitions as:
PROJECT_ROOT = os.path.dirname(os.path.abspath(__file__))
BASE_DIR = os.path.dirname(PROJECT_ROOT)
because the os.path.dirname()
function simply removes the last segment of a path.In the above, the __file__
name points to the filename of the current module, see the Python datamodel:
However, it can be a relative path, so the
__file__
is the pathname of the file from which the module was loaded, if it was loaded from a file.
os.path.abspath()
function is used to turn that into an absolute path before removing just the filename and storing the full path to the directory the module lives in in PROJECT_ROOT
. What does os.path.abspath(os.path.join(os.path.dirname(__file__), os.path.pardir)) mean? python
That is a clever way to refer to paths regardless of the script location. The cryptic line you're referring is:
os.path.abspath(os.path.join(os.path.dirname(__file__), os.path.pardir))
There are 3 methods and a 2 constants present:abspath
returns absolute path of a pathjoin
join to path stringsdirname
returns the directory of a file__file__
refers to thescript
's file namepardir
returns the representation of a parent directory in the OS (usually..
)
There might be other approaches to get a parent directory of where a file is located, for example, programs have the concept of current working directory, os.getcwd()
. So doing os.getcwd()+'/..'
might work. But this is very dangerous, because working directories can be changed.
Also, if the file is intended to be imported, the working directory will point to the importing file, not the importee, but __file__
always points to the actual module's file so it is safer.
Hope this helps!
Edit: P.S. - Python 3 greatly simplifies this situation by letting us treat paths in an object-oriented manner, so the above line becomes:
from pathlib import Path
Path(__file__).resolve().parent.parent
How to get the parent dir location
You can apply dirname repeatedly to climb higher: dirname(dirname(file))
. This can only go as far as the root package, however. If this is a problem, use os.path.abspath
: dirname(dirname(abspath(file)))
.
Related Topics
How to Release Memory After Creating Matplotlib Figures
Counting Cars Opencv + Python Issue
Broken References in Virtualenvs
Multiple Plots in One Figure in Python
Os.Path.Dirname(_File_) Returns Empty
Multiprocessing.Pool Makes Numpy Matrix Multiplication Slower
How to Access Class Member Variables in Python
How to Explain the Reverse of a Sequence by Slice Notation A[::-1]
How to Tell Pycharm What Type a Parameter Is Expected to Be
How to Crop an Image Using Pil
Is There a Numpy Builtin to Reject Outliers from a List
What Is the Official "Preferred" Way to Install Pip and Virtualenv Systemwide
Convert Categorical Data in Pandas Dataframe
Differencebetween 'Py' and 'Python' in the Windows Terminal