Skip to main content
added 165 characters in body
Source Link
Asclepius
  • 64.9k
  • 20
  • 188
  • 166

mkpath creates the nested directory, and does nothing if the directory already exists. This works in both Python 2 and 3. Note however that distutils has been deprecated, and is scheduled for removal in Python 3.12.

mkpath creates the nested directory, and does nothing if the directory already exists. This works in both Python 2 and 3.

mkpath creates the nested directory, and does nothing if the directory already exists. This works in both Python 2 and 3. Note however that distutils has been deprecated, and is scheduled for removal in Python 3.12.

added 79 characters in body
Source Link
Asclepius
  • 64.9k
  • 20
  • 188
  • 166

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.

If using Python 3.4, even though it comes with pathlib, the backport is intended to offer a newer and superior implementation of mkdir.

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.

added 143 characters in body
Source Link
Asclepius
  • 64.9k
  • 20
  • 188
  • 166

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, the backport is intended to offer a newer and superior implementation of mkdir.

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.

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.

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.

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, the backport is intended to offer a newer and superior implementation of mkdir.

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.

added 270 characters in body
Source Link
Asclepius
  • 64.9k
  • 20
  • 188
  • 166
Loading
added 270 characters in body
Source Link
Asclepius
  • 64.9k
  • 20
  • 188
  • 166
Loading
edited body
Source Link
Asclepius
  • 64.9k
  • 20
  • 188
  • 166
Loading
added 14 characters in body
Source Link
Asclepius
  • 64.9k
  • 20
  • 188
  • 166
Loading
added 83 characters in body
Source Link
Asclepius
  • 64.9k
  • 20
  • 188
  • 166
Loading
deleted 85 characters in body
Source Link
Asclepius
  • 64.9k
  • 20
  • 188
  • 166
Loading
deleted 85 characters in body
Source Link
Asclepius
  • 64.9k
  • 20
  • 188
  • 166
Loading
deleted 85 characters in body
Source Link
Asclepius
  • 64.9k
  • 20
  • 188
  • 166
Loading
deleted 75 characters in body
Source Link
Asclepius
  • 64.9k
  • 20
  • 188
  • 166
Loading
added 11 characters in body
Source Link
Asclepius
  • 64.9k
  • 20
  • 188
  • 166
Loading
deleted 20 characters in body
Source Link
Asclepius
  • 64.9k
  • 20
  • 188
  • 166
Loading
added 75 characters in body
Source Link
Asclepius
  • 64.9k
  • 20
  • 188
  • 166
Loading
added 1 character in body
Source Link
Asclepius
  • 64.9k
  • 20
  • 188
  • 166
Loading
added 201 characters in body
Source Link
Asclepius
  • 64.9k
  • 20
  • 188
  • 166
Loading
added 142 characters in body
Source Link
Asclepius
  • 64.9k
  • 20
  • 188
  • 166
Loading
added 17 characters in body
Source Link
Asclepius
  • 64.9k
  • 20
  • 188
  • 166
Loading
added 29 characters in body
Source Link
Asclepius
  • 64.9k
  • 20
  • 188
  • 166
Loading
added 5 characters in body
Source Link
Asclepius
  • 64.9k
  • 20
  • 188
  • 166
Loading
added 5 characters in body
Source Link
Asclepius
  • 64.9k
  • 20
  • 188
  • 166
Loading
added 97 characters in body
Source Link
Asclepius
  • 64.9k
  • 20
  • 188
  • 166
Loading
added 6 characters in body
Source Link
Asclepius
  • 64.9k
  • 20
  • 188
  • 166
Loading
edited body
Source Link
Asclepius
  • 64.9k
  • 20
  • 188
  • 166
Loading