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?
I use
os.path.exists()
, here is a Python 3 script that can be used to check if a directory exists, create one if it does not exist, and delete it if it does exist (if desired).It prompts users for input of the directory and can be easily modified.
Call the function
create_dir()
at the entry point of your program/project.I see two answers with good qualities, each with a small flaw, so I will give my take on it:
Try
os.path.exists
, and consideros.makedirs
for the creation.As noted in comments and elsewhere, there's a race condition – if the directory is created between the
os.path.exists
and theos.makedirs
calls, theos.makedirs
will fail with anOSError
. Unfortunately, blanket-catchingOSError
and continuing is not foolproof, as it will ignore a failure to create the directory due to other factors, such as insufficient permissions, full disk, etc.One option would be to trap the
OSError
and examine the embedded error code (see Is there a cross-platform way of getting information from Python’s OSError):Alternatively, there could be a second
os.path.exists
, but suppose another created the directory after the first check, then removed it before the second one – we could still be fooled.Depending on the application, the danger of concurrent operations may be more or less than the danger posed by other factors such as file permissions. The developer would have to know more about the particular application being developed and its expected environment before choosing an implementation.
You can use
os.listdir
for this:I would personally recommend that you use
os.path.isdir()
to test instead ofos.path.exists()
.If you have:
And a foolish user input:
... You're going to end up with a directory named
filename.etc
when you pass that argument toos.makedirs()
if you test withos.path.exists()
.The direct answer to this is, assuming a simple situation where you don't expect other users or processes to be messing with your directory:
or if making the directory is subject to race conditions (i.e. if after checking the path exists, something else may have already made it) do this:
But perhaps an even better approach is to sidestep the resource contention issue, by using temporary directories via
tempfile
:Here's the essentials from the online doc:
New in Python 3.5:
pathlib.Path
withexist_ok
There's a new
Path
object (as of 3.4) with lots of methods one would want to use with paths - one of which ismkdir
.(For context, I'm tracking my weekly rep with a script. Here's the relevant parts of code from the script that allow me to avoid hitting Stack Overflow more than once a day for the same data.)
First the relevant imports:
We don't have to deal with
os.path.join
now - just join path parts with a/
:Then I idempotently ensure the directory exists - the
exist_ok
argument shows up in Python 3.5:Here's the relevant part of the documentation:
Here's a little more of the script - in my case, I'm not subject to a race condition, I only have one process that expects the directory (or contained files) to be there, and I don't have anything trying to remove the directory.
Path
objects have to be coerced tostr
before other APIs that expectstr
paths can use them.Perhaps Pandas should be updated to accept instances of the abstract base class,
os.PathLike
.