👻 Check our latest review to choose the best laptop for Machine Learning engineers and Deep learning tasks!
What is the most elegant way to check if the directory a file is going to be written to exists, and if not, create the directory using Python? Here is what I tried:
import os
file_path = "/my/directory/filename.txt"
directory = os.path.dirname(file_path)
try:
os.stat(directory)
except:
os.mkdir(directory)
f = file(filename)
Somehow, I missed os.path.exists
(thanks kanja, Blair, and Douglas). This is what I have now:
def ensure_dir(file_path):
directory = os.path.dirname(file_path)
if not os.path.exists(directory):
os.makedirs(directory)
Is there a flag for "open", that makes this happen automatically?
👻 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 can I safely create a nested directory in Python?, 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 can I safely create a nested directory in Python?
- Deutsch How can I safely create a nested directory in Python?
- Français How can I safely create a nested directory in Python?
- Español How can I safely create a nested directory in Python?
- Türk How can I safely create a nested directory in Python?
- Русский How can I safely create a nested directory in Python?
- Português How can I safely create a nested directory in Python?
- Polski How can I safely create a nested directory in Python?
- Nederlandse How can I safely create a nested directory in Python?
- 中文 How can I safely create a nested directory in Python?
- 한국어 How can I safely create a nested directory in Python?
- 日本語 How can I safely create a nested directory in Python?
- हिन्दी How can I safely create a nested directory in Python?
California | 2023-01-27
I was preparing for my coding interview, thanks for clarifying this - How can I safely create a nested directory in Python? in Python is not the simplest one. Will use it in my bachelor thesis
Texas | 2023-01-27
Maybe there are another answers? What How can I safely create a nested directory in Python? exactly means?. Checked yesterday, it works!
Boston | 2023-01-27
I was preparing for my coding interview, thanks for clarifying this - How can I safely create a nested directory in Python? in Python is not the simplest one. I just hope that will not emerge anymore