In several SO's question there is these lines to access the parent directory of the code, e.g. os.path.join(os.path.dirname(__file__)) returns nothing and os.path.join(os.path.dirname(__file__)) returns nothing
import os, sys parentddir = os.path.abspath(os.path.join(os.path.dirname(__file__), os.path.pardir)) sys.path.append(parentddir)
I understand that os.path.abspath()
returns absolute path of something and sys.path.append()
adds the path for the code to access. but what is this cryptic line below, what does it really mean?
os.path.abspath(os.path.join(os.path.dirname(__file__), os.path.pardir))
Is there another way to achieve the same purpose of appending the parent directory of the where the code?
This problem happens because I am calling functions across directories and sometimes they share the same file name, e.g. script1/utils.py
and script2/utils.py
. I am calling a function from script1/test.py
which calls script2/something.py
contains a function that calls script2/utils.py
and the following code
script1/ utils.py src/ test.py script2/ utils.py code/ something.py
test.py
from script2.code import something import sys sys.path.append('../') import utils something.foobar()
something.py
import os, sys parentddir = os.path.abspath(os.path.join(os.path.dirname(__file__), os.path.pardir)) sys.path.append(parentddir) import utils def foobar(): utils.somefunc()
path. abspath() returns a normalized absolutized version of the pathname path which may sound fancy but it simply means that this method returns the pathname to the path passed as a parameter to this function.
path. dirname() method in Python is used to get the directory name from the specified path.
path. join() method in Python join one or more path components intelligently. This method concatenates various path components with exactly one directory separator ('/') following each non-empty part except the last path component.
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.
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 the script
's file namepardir
returns the representation of a parent directory in the OS (usually ..
)Thus, the expression returns the full path name of the executing script in a multiplatform-safe way. No need to hardwire any directions, that's why it is so useful.
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
__file__
represents the file the code is executing from
os.path.dirname(__file__)
gives you the directory the file is in
os.path.pardir
stands for ".." which means one directory above the current one
os.path.join(os.path.dirname(__file__), os.path.pardir)
joins the directory name and ".."
os.path.abspath(os.path.join(os.path.dirname(__file__), os.path.pardir))
resolves the above path and gives you an absolute path for the parent directory of the directory your file is in
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With