[New-bugs-announce] [issue25583] os.makedirs with exist_ok=True raises PermissionError on Windows 7^

Daniel Plachotich report at bugs.python.org
Sun Nov 8 09:34:33 EST 2015


New submission from Daniel Plachotich:

Since Windows 7 (or even Vista), Windows gives permission error(5, ERROR_ACCESS_DENIED
if you try to create a directory in a drive root with the same name as a drive itself,
even if you have administrative permissions. This behavior is not mentioned in Microsoft docs.

Here is an example session (Windows 7, admin):
d:\>IF EXIST . echo True
True
d:\>mkdir .
Access is denied.
d:\>mkdir dir
d:\>cd dir
d:\dir>mkdir .
A subdirectory or file . already exists.
d:\dir>cd ..
d:\>
d:\>py -3
Python 3.4.3 (v3.4.3:9b73f1c3e601, Feb 24 2015, 22:43:06) [MSC v.1600 32 bit (In
tel)] on win32
>>> import os
>>> os.path.isdir('.')
True
>>> os.makedirs('.', exist_ok=True)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "C:\Python34\lib\os.py", line 237, in makedirs
    mkdir(name, mode)
PermissionError: [WinError 5] ...
>>> try:
...   os.mkdir('.')
... except OSError as e:
...   print(e.errno)
...
13

This means that if you want to write portable code, you still need to write like in Python 2:
    if not os.path.isdir(path):
        os.makedirs(path)
Which makes exist_ok useless.

The actual problem is in this line (Lib/os.py#l243):
    if not exist_ok or e.errno != errno.EEXIST or not path.isdir(name):

Due the reasons described above, makedirs shouldn't rely on e.errno, so the right code will be:
    if not (exist_ok and path.isdir(name)):

I think the issue is pretty serious to be backported.

----------
components: Library (Lib)
messages: 254339
nosy: Daniel Plachotich
priority: normal
severity: normal
status: open
title: os.makedirs with exist_ok=True raises PermissionError on Windows 7^
type: behavior
versions: Python 3.2, Python 3.3, Python 3.4, Python 3.5, Python 3.6

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


More information about the New-bugs-announce mailing list