👻 Check our latest review to choose the best laptop for Machine Learning engineers and Deep learning tasks!
this code is get the templates/blog1/page.html in b.py:
path = os.path.join(os.path.dirname(__file__), os.path.join("templates", "blog1/page.html"))
but i want to get the parent dir location:
aParent
|--a
| |---b.py
| |---templates
| |--------blog1
| |-------page.html
|--templates
|--------blog1
|-------page.html
and how to get the aParent location
thanks
updated:
this is right:
dirname=os.path.dirname
path = os.path.join(dirname(dirname(__file__)), os.path.join("templates", "blog1/page.html"))
or
path = os.path.abspath(os.path.join(os.path.dirname(__file__),".."))
👻 Read also: what is the best laptop for engineering students?
os.path.dirname(__file__) returns empty
2 answers
I want to get the path of the current directory under which a .py file is executed.
For example a simple file D: est.py
with code:
import os
print os.getcwd()
print os.path.basename(__file__)
print os.path.abspath(__file__)
print os.path.dirname(__file__)
It is weird that the output is:
D:
test.py
D: est.py
EMPTY
I am expecting the same results from the getcwd()
and path.dirname()
.
Given os.path.abspath = os.path.dirname + os.path.basename
, why
os.path.dirname(__file__)
returns empty?
Answer #1
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__))
What is the difference between os.path.basename() and os.path.dirname()?
2 answers
What is the difference between os.path.basename()
and os.path.dirname()
?
I already searched for answers and read some links, but didn"t understand. Can anyone give a simple explanation?
Answer #1
Both functions use the os.path.split(path)
function to split the pathname path
into a pair; (head, tail)
.
The os.path.dirname(path)
function returns the head of the path.
E.g.: The dirname of "/foo/bar/item"
is "/foo/bar"
.
The os.path.basename(path)
function returns the tail of the path.
E.g.: The basename of "/foo/bar/item"
returns "item"
From: http://docs.python.org/3/library/os.path.html#os.path.basename
We hope this article has helped you to resolve the problem. Apart from How to get the parent dir location, check other code Python module-related topics.
Want to excel in Python? See our review of the best Python online courses 2023. If you are interested in Data Science, check also how to learn programming in R.
By the way, this material is also available in other languages:
- Italiano How to get the parent dir location
- Deutsch How to get the parent dir location
- Français How to get the parent dir location
- Español How to get the parent dir location
- Türk How to get the parent dir location
- Русский How to get the parent dir location
- Português How to get the parent dir location
- Polski How to get the parent dir location
- Nederlandse How to get the parent dir location
- 中文 How to get the parent dir location
- 한국어 How to get the parent dir location
- 日本語 How to get the parent dir location
- हिन्दी How to get the parent dir location
New York | 2023-01-27
Simply put and clear. Thank you for sharing. How to get the parent dir location and other issues with html Python module was always my weak point 😁. Will use it in my bachelor thesis
Prague | 2023-01-27
Maybe there are another answers? What How to get the parent dir location exactly means?. Checked yesterday, it works!
London | 2023-01-27
Maybe there are another answers? What How to get the parent dir location exactly means?. I just hope that will not emerge anymore