This issue tracker has been migrated to GitHub, and is currently read-only.
For more information, see the GitHub FAQs in the Python's Developer Guide.

classification
Title: pathlib's Path.mkdir() should allow for "mkdir -p" functionality
Type: enhancement Stage: resolved
Components: Library (Lib) Versions: Python 3.5
process
Status: closed Resolution: fixed
Dependencies: Superseder:
Assigned To: barry Nosy List: barry, berker.peksag, garrison, pitrou, python-dev
Priority: normal Keywords: patch

Created on 2014-05-20 03:53 by garrison, last changed 2022-04-11 14:58 by admin. This issue is now closed.

Files
File name Uploaded Description Edit
issue21539.diff berker.peksag, 2014-05-20 12:05 review
issue21539_v2.diff berker.peksag, 2014-05-31 18:42 review
Messages (8)
msg218832 - (view) Author: Jim Garrison (garrison) Date: 2014-05-20 03:53
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.
msg218842 - (view) Author: Antoine Pitrou (pitrou) * (Python committer) Date: 2014-05-20 11:46
I think we can a new `exist_ok` parameter indeed. It should probably be False by default, though.
msg219233 - (view) Author: Barry A. Warsaw (barry) * (Python committer) Date: 2014-05-27 18:32
I've just been writing some new code to use pathlib and ran into this one myself.  An exist_ok=False is fine, although it's a slight shame that for backward compatibility we can't adopt os.makedirs() signature exactly.
msg219238 - (view) Author: Antoine Pitrou (pitrou) * (Python committer) Date: 2014-05-27 20:48
Le 27/05/2014 20:32, Barry A. Warsaw a écrit :
>
> I've just been writing some new code to use pathlib and ran into
> this
one myself. An exist_ok=False is fine, although it's a slight shame that
for backward compatibility we can't adopt os.makedirs() signature exactly.

What do you mean by that? The os.makedirs() signature is 
os.makedirs(name, mode=0o777, exist_ok=False)
msg219239 - (view) Author: Barry A. Warsaw (barry) * (Python committer) Date: 2014-05-27 21:06
On May 27, 2014, at 08:48 PM, Antoine Pitrou wrote:

>What do you mean by that? The os.makedirs() signature is 
>os.makedirs(name, mode=0o777, exist_ok=False)

Right, but this is Path.mkdir's signature:

Path.mkdir(mode=0o777, parents=False)

so it's too late to make exist_ok=False the second argument (i.e. the one
after `mode`).  Oh well.
msg219468 - (view) Author: Berker Peksag (berker.peksag) * (Python committer) Date: 2014-05-31 18:42
Updated patch. Thank you Barry and Antoine.
msg224839 - (view) Author: Barry A. Warsaw (barry) * (Python committer) Date: 2014-08-05 14:52
I like it.  Thanks for the contribution!  Assigning to myself since I plan on working to land this in 3.5.
msg224842 - (view) Author: Roundup Robot (python-dev) (Python triager) Date: 2014-08-05 15:28
New changeset 8af80d68bcdc by Barry Warsaw in branch 'default':
- Issue #21539: Add a *exists_ok* argument to `Pathlib.mkdir()` to mimic
http://hg.python.org/cpython/rev/8af80d68bcdc
History
Date User Action Args
2022-04-11 14:58:03adminsetgithub: 65738
2014-08-05 18:29:38berker.peksagsetstage: patch review -> resolved
2014-08-05 15:29:26barrysetstatus: open -> closed
resolution: fixed
2014-08-05 15:28:55python-devsetnosy: + python-dev
messages: + msg224842
2014-08-05 14:52:22barrysetassignee: barry
messages: + msg224839
2014-05-31 18:42:36berker.peksagsetfiles: + issue21539_v2.diff

messages: + msg219468
stage: patch review
2014-05-27 21:06:55barrysetmessages: + msg219239
2014-05-27 20:48:31pitrousetmessages: + msg219238
2014-05-27 18:32:01barrysetmessages: + msg219233
2014-05-27 18:30:36barrysetnosy: + barry
2014-05-20 12:05:34berker.peksagsetfiles: + issue21539.diff
nosy: + berker.peksag
keywords: + patch
2014-05-20 11:46:05pitrousetmessages: + msg218842
versions: + Python 3.5
2014-05-20 10:48:53pitrousetnosy: + pitrou
2014-05-20 03:53:56garrisoncreate