How can I safely create a nested directory?

python create directory if not exists
python create folder in current directory
how to create folders and subfolders in python
how to create dynamic folder in python
python create file and directory
python create directory recursively
python create subdirectory
create directory in django

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 see two answers with good qualities, each with a small flaw, so I will give my take on it:

Try os.path.exists, and consider os.makedirs for the creation.

import os
if not os.path.exists(directory):
    os.makedirs(directory)

As noted in comments and elsewhere, there's a race condition – if the directory is created between the os.path.exists and the os.makedirs calls, the os.makedirs will fail with an OSError. Unfortunately, blanket-catching OSError 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):

import os, errno

try:
    os.makedirs(directory)
except OSError as e:
    if e.errno != errno.EEXIST:
        raise

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.

Modern versions of Python improve this code quite a bit, both by exposing FileExistsError (in 3.3+)...

try:
    os.makedirs("path/to/directory")
except FileExistsError:
    # directory already exists
    pass

...and by allowing a keyword argument to os.makedirs called exist_ok (in 3.2+).

os.makedirs("path/to/directory", exist_ok=True)  # succeeds even if directory exists.

How to Create Multiple Subdirectories with One Linux Command, first if it doesn't already exist (htg, in our case). How can I safely create a nested directory, If you are searching how to safely create a nested directory in Python, use the following example code:

Python 3.5+:
import pathlib
pathlib.Path('/my/directory').mkdir(parents=True, exist_ok=True) 

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 the parents argument.

Python 3.2+:

Using pathlib:

If you can, install the current pathlib backport named pathlib2. Do not install the older unmaintained backport named pathlib. 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 useful exist_ok option. The backport is intended to offer a newer and superior implementation of mkdir which includes this missing option.

Using os:

import os
os.makedirs(path, exist_ok=True)

os.makedirs as used above recursively creates the directory and does not raise an exception if the directory already exists. It has the optional exist_ok argument only if using Python 3.2+, with a default value of False. 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 named pathlib2. Do not install the older unmaintained backport named pathlib. Next, refer to the Python 3.5+ section above and use it the same.

Using os:

import os
try: 
    os.makedirs(path)
except OSError:
    if not os.path.isdir(path):
        raise

While a naive solution may first use os.path.isdir followed by os.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 because OSError: [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.

import distutils.dir_util
distutils.dir_util.mkpath(path)

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.

Nested folder dictionary definition, is a "subfolder," and subfolders can also contain subfolders and so on up to a maximum level. After three or four levels, it becomes cumbersome for users to deal with. How can I safely create a nested directory? +1 vote . 2 views. asked May 23, 2019 in Python by Nigam (4.1k points) edited Jun 2, 2019 by Nigam.

Using try except and the right error code from errno module gets rid of the race condition and is cross-platform:

import os
import errno

def make_sure_path_exists(path):
    try:
        os.makedirs(path)
    except OSError as exception:
        if exception.errno != errno.EEXIST:
            raise

In other words, we try to create the directories, but if they already exist we ignore the error. On the other hand, any other error gets reported. For example, if you create dir 'a' beforehand and remove all permissions from it, you will get an OSError raised with errno.EACCES (Permission denied, error 13).

How To Safely Create Nested Directories in Python, How do I create a directory inside a directory in Linux? 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.

I would personally recommend that you use os.path.isdir() to test instead of os.path.exists().

>>> os.path.exists('/tmp/dirname')
True
>>> os.path.exists('/tmp/dirname/filename.etc')
True
>>> os.path.isdir('/tmp/dirname/filename.etc')
False
>>> os.path.isdir('/tmp/fakedirname')
False

If you have:

>>> dir = raw_input(":: ")

And a foolish user input:

:: /tmp/dirname/filename.etc

... You're going to end up with a directory named filename.etc when you pass that argument to os.makedirs() if you test with os.path.exists().

How can I safely create a nested directory?, mkdir() method offer a technique to safely create a nested directory. We begin by importing the Path class from pathlib . When creating the Path object, include the directory path to be created. Next, we'll use the . Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Check os.makedirs: (It makes sure the complete path exists.) To handle the fact the directory might exist, catch OSError. (If exist_ok is False (the default), an OSError is raised if the target directory already exists.)

import os
try:
    os.makedirs('./path/to/somewhere')
except OSError:
    pass

How to create a nested directory in Python, safely, There are several methods to perform this task, I will discuss a few with you: You can try os.path.existsand for creation consider os.makedirsfor  ant way to check if the directory a file is going to be written to exists, and if not, create the directory using Python? How to safely create a nested directory

Python How to Safely Create a Nested Directory, While file manipulation with Python is very easy, there are actually a lot of edge cases to be considered while creating directories and ensuring that your file  How can I safely create a nested directory in Python? - 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

How to create nested directory in a single command?, Python How to Safely Create a Nested Directory import os os.getcwd() if not os.​path.exists(dir Duration: 6:21 Posted: Dec 29, 2018 You can do it using the [code ]dir.create[/code] function and setting the [code ]recursive[/code] parameter to [code ]TRUE[/code], as follows: [code]dir.create(&#039

How can I create a directory if it does not exist using Python?, The command you are looking for is mkdir -p x/y/z . The -p switch create parents directories. ~$ mkdir -p d/s/a/e ~$ cd d/s/a/e/ ~/d/s/a/e$. While file manipulation with Python is very easy, there are actually a lot of edge cases to be considered while creating directories and ensuring that your file structure isn’t marred when you run the code on some other machine.

Comments
  • In general you might need to account for the case where there's no directory in the filename. On my machine dirname('foo.txt') gives '', which doesn't exist and causes makedirs() to fail.
  • In python 2.7 os.path.mkdir doesn't exist. It's os.mkdir.
  • if the path exists one has not only to check if it is a directory and not a regular file or another object (many answers check this) it is also necessary to check if it is writable (I did not find an answer that checked this)
  • In case you came here to create parent directories of file path string p, here is my code snippet: os.makedirs(p[:p.rindex(os.path.sep)], exist_ok=True)
  • meta discussion of answers in this question
  • The race condition is a good point, but the approach in stackoverflow.com/questions/273192/#273208, will mask a failure to create the directory. Don't feel bad for voting down - you don't like the answer. It's what votes are for.
  • Remember that os.path.exists() isn't free. If the normal case is that the directory will be there, then the case where it isn't should be handled as an exception. In other words, try to open and write to your file, catch the OSError exception and, based on errno, do your makedir() and re-try or re-raise. This creates duplication of code unless you wrap the writing in a local method.
  • os.path.exists also returns True for a file. I have posted an answer to address this.
  • As commenters to other answers here have noted, the exists_ok parameter to os.makedirs() can be used to cover how prior existence of the path is handled, since Python 3.2.
  • os.mkdirs() can create unintended folders if a path separator is accidentally left out, the current folder is not as expected, a path element contains the path separator. If you use os.mkdir() these bugs will raise an exception, alerting you to their existence.