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?
This will check if the file is there if it is not then it will create it.
I saw Heikki Toivonen and A-B-B's answers and thought of this variation.
Insights on the specifics of this situation
You give a particular file at a certain path and you pull the directory from the file path. Then after making sure you have the directory, you attempt to open a file for reading. To comment on this code:
We want to avoid overwriting the builtin function,
dir
. Also,filepath
or perhapsfullfilepath
is probably a better semantic name thanfilename
so this would be better written:Your end goal is to open this file, you initially state, for writing, but you're essentially approaching this goal (based on your code) like this, which opens the file for reading:
Assuming opening for reading
Why would you make a directory for a file that you expect to be there and be able to read?
Just attempt to open the file.
If the directory or file isn't there, you'll get an
IOError
with an associated error number:errno.ENOENT
will point to the correct error number regardless of your platform. You can catch it if you want, for example:Assuming we're opening for writing
This is probably what you're wanting.
In this case, we probably aren't facing any race conditions. So just do as you were, but note that for writing, you need to open with the
w
mode (ora
to append). It's also a Python best practice to use the context manager for opening files.However, say we have several Python processes that attempt to put all their data into the same directory. Then we may have contention over creation of the directory. In that case it's best to wrap the
makedirs
call in a try-except block.Python 3.5+:
pathlib.Path.mkdir
as used above recursively creates the directory and does not raise an exception if the directory already exists. If you don't need or want the parents to be created, skip theparents
argument.Python 3.2+:
Using
pathlib
:If you can, install the current
pathlib
backport namedpathlib2
. Do not install the older unmaintained backport namedpathlib
. Next, refer to the Python 3.5+ section above and use it the same.If using Python 3.4, even though it comes with
pathlib
, it is missing the usefulexist_ok
option. The backport is intended to offer a newer and superior implementation ofmkdir
which includes this missing option.Using
os
:os.makedirs
as used above recursively creates the directory and does not raise an exception if the directory already exists. It has the optionalexist_ok
argument only if using Python 3.2+, with a default value ofFalse
. This argument does not exist in Python 2.x up to 2.7. As such, there is no need for manual exception handling as with Python 2.7.Python 2.7+:
Using
pathlib
:If you can, install the current
pathlib
backport namedpathlib2
. Do not install the older unmaintained backport namedpathlib
. Next, refer to the Python 3.5+ section above and use it the same.Using
os
:While a naive solution may first use
os.path.isdir
followed byos.makedirs
, the solution above reverses the order of the two operations. In doing so, it prevents a common race condition having to do with a duplicated attempt at creating the directory, and also disambiguates files from directories.Note that capturing the exception and using
errno
is of limited usefulness becauseOSError: [Errno 17] File exists
, i.e.errno.EEXIST
, is raised for both files and directories. It is more reliable simply to check if the directory exists.Alternative:
mkpath
creates the nested directory, and does nothing if the directory already exists. This works in both Python 2 and 3.Per Bug 10948, a severe limitation of this alternative is that it works only once per python process for a given path. In other words, if you use it to create a directory, then delete the directory from inside or outside Python, then use
mkpath
again to recreate the same directory,mkpath
will simply silently use its invalid cached info of having previously created the directory, and will not actually make the directory again. In contrast,os.makedirs
doesn't rely on any such cache. This limitation may be okay for some applications.With regard to the directory's mode, please refer to the documentation if you care about it.
You can use
mkpath
Note that it will create the ancestor directories as well.
It works for Python 2 and 3.
If you consider the following:
means a directory (path) exists AND is a directory. So for me this way does what I need. So I can make sure it is folder (not a file) and exists.