I’m not sure which of these two osmethods to use to make a new directory.
Please provide me with a Python sample.
Asked by Toni
If no intermediate directories exist, makedirs() creates them (just like mkdir -p in bash).
mkdir() can only create one subdirectory and will throw an exception if intermediate directories are supplied that do not exist.
To make a single ‘leaf’ directory (dirA), use either:
Makedirs, on the other hand, must be used to create ‘branches’:
If dirA already exists, mkdir will function, but if it doesn’t, an error will be thrown.
It’s worth noting that, unlike mkdir -p in bash, neither of these commands will succeed if the leaf already exists.
Answered by NPE
(I can’t remark; I can just contribute to NPE’s response.)
exist ok=False is the default option for os.makedirs in Python 3. If True, os.makedirs will not throw an exception if the leaf already exists. (This parameter is missing from os.mkdir.)
Just like this:
P.S. In IPython shell, type? before the name of a method to have a fast peek at the documentation. e.g.:
>>> import os >>> ? os.makedirs
Answered by Yunqing Gong
Post is based on https://stackoverflow.com/questions/13819496/what-is-different-between-makedirs-and-mkdir-of-os