[issue21539] pathlib's Path.mkdir() should allow for "mkdir -p" functionality

Jim Garrison report at bugs.python.org
Tue May 20 05:53:56 CEST 2014


New submission from Jim Garrison:

As of Python 3.2, `os.makedirs()` takes `exist_ok` as a keyword argument.  If set to true, the function does not raise an error if the directory already exists.  This makes the function's behavior similar to `mkdir -p` on the UNIX commandline.  (See https://docs.python.org/3/library/os.html#os.makedirs)

However, this functionality is missing in Python's new pathlib. In fact, the documentation for `Path.mkdir()` even mentions that if `parent=True` is passed, then it should behave like `mkdir -p`.  This is accurate in that it indeed recursively makes any parent directories necessary, but it is inaccurate in that it raises an error if the directory already exists.

I propose that either `parents=True` should imply that there is no error if the directory already exists (which would be a backwards-incompatible change); or, there could be a new `exist_ok=True`, which could suppress the error for an existing directory.  Either way, it ought to be possible to get the `mkdir -p` functionality easily from pathlib.  And the documentation for `Path.mkdir()` should be updated to explain how to get this `mkdir -p` functionality, with the existing (inaccurate) `mkdir -p` mention removed.

----------
components: Library (Lib)
messages: 218832
nosy: garrison
priority: normal
severity: normal
status: open
title: pathlib's Path.mkdir() should allow for "mkdir -p" functionality
type: enhancement

_______________________________________
Python tracker <report at bugs.python.org>
<http://bugs.python.org/issue21539>
_______________________________________


More information about the Python-bugs-list mailing list