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: Add option to os.mkdir to not raise an exception for existing directories
Type: enhancement Stage:
Components: Library (Lib) Versions: Python 3.4
process
Status: closed Resolution: wont fix
Dependencies: Superseder:
Assigned To: Nosy List: hynek, loewis, r.david.murray, rhettinger, serhiy.storchaka
Priority: normal Keywords:

Created on 2012-06-16 07:10 by rhettinger, last changed 2022-04-11 14:57 by admin. This issue is now closed.

Messages (6)
msg162956 - (view) Author: Raymond Hettinger (rhettinger) * (Python committer) Date: 2012-06-16 07:10
I commonly see the code pattern:

try:
    os.mkdir(dirname)
except OSError:
    pass

It would be nice to have this instead:

   os.mkdir(dirname, ignore_if_existing=True)

There's probably a better name for the keyword argument, but it communicates the idea clearly enough.
msg162958 - (view) Author: Serhiy Storchaka (serhiy.storchaka) * (Python committer) Date: 2012-06-16 07:47
Agree. First, the use of parameter is shorter (1 line instead of 4). Second, try/except idiom hides possible real OS errors (no rights to create the directory, a non-directory file with the same name already exists, illegal filename, too long patch, etc.).

But "ignore_if_existing" is not good name for my taste.
msg162959 - (view) Author: Hynek Schlawack (hynek) * (Python committer) Date: 2012-06-16 08:06
+1.

Raymond, is there a reason you put that for 3.4? I'd try to get it in before beta1 next week, it's trivial enough.

Regarding the name: os.makedirs() uses exist_ok, we shouldn't invent new ones.
msg162963 - (view) Author: Hynek Schlawack (hynek) * (Python committer) Date: 2012-06-16 10:03
I looked into the code. Assuming it should be added, we're facing the fact that os.mkdir() is C code ATM and the handling of the error would require to implement it for NT and Unix separately.

Therefore it would make sense to add a Python os.mkdir() that handles this (basically try: _mkdir(dirname) except FileExistsError: pass) and make the C implementation private.

OTOH you could argue that if you just want to ensure that that a directory exists, you could just as well use os.makedirs() with exists_ok so I'm kind of -0 on the whole thing.
msg162964 - (view) Author: Martin v. Löwis (loewis) * (Python committer) Date: 2012-06-16 10:33
I agree that this is already covered by the exist_ok parameter (which is new to 3.2). Existing code just hasn't been ported to this API.
msg162972 - (view) Author: R. David Murray (r.david.murray) * (Python committer) Date: 2012-06-16 13:54
Note that the implementation of makedirs exist_ok has a significant bug (issue 13498) which renders it mostly useless in practice.  Parties interested in this issue might be interested in figuring out how to fix that bug :)
History
Date User Action Args
2022-04-11 14:57:31adminsetgithub: 59289
2012-06-16 13:54:07r.david.murraysetnosy: + r.david.murray
messages: + msg162972
2012-06-16 10:33:49loewissetstatus: open -> closed
resolution: wont fix
messages: + msg162964
2012-06-16 10:03:18hyneksetassignee: hynek ->

messages: + msg162963
nosy: + loewis
2012-06-16 08:29:05hyneksettitle: Add option to os.makefile to not raise an exception for existing directories -> Add option to os.mkdir to not raise an exception for existing directories
2012-06-16 08:06:30hyneksetassignee: hynek

messages: + msg162959
nosy: + hynek
2012-06-16 07:47:34serhiy.storchakasetnosy: + serhiy.storchaka
messages: + msg162958
components: + Library (Lib)
2012-06-16 07:10:49rhettingercreate