[New-bugs-announce] [issue35192] pathlib mkdir throws FileExistsError when not supposed to

Adam Dunlap report at bugs.python.org
Thu Nov 8 15:08:28 EST 2018

New submission from Adam Dunlap <dunlapadam at gmail.com>:

I have 2 processes (one C, one python) that are started at the same time and both need a certain directory tree to exist to continue, so both processes create the directory tree while ignoring errors about it already existing.

In the python process, I'm using pathlib's mkdir function (https://docs.python.org/3/library/pathlib.html#pathlib.Path.mkdir) with parents=True, exist_ok=True, but this is sometimes throwing a FileExistsError. I don't expect this because the documentation says "If exist_ok is true, FileExistsError exceptions will be ignored (same behavior as the POSIX mkdir -p command), but only if the last path component is not an existing non-directory file." The last component is never a non-directory file.

I believe what is happening is that mkdir realizes that the parent doesn't exist, so it recursively tries to make the parent directory. However, when it recurses, it uses the default exists_ok value of False. Before the recursive call can make the parent directory, the other process makes the directory. This causes the inner call to throw a FileExistsError.

I believe the correct thing to do is to always pass True for exists_ok in the recursive call.

components: Library (Lib)
messages: 329484
nosy: Adam Dunlap
priority: normal
severity: normal
status: open
title: pathlib mkdir throws FileExistsError when not supposed to
type: behavior
versions: Python 3.5

Python tracker <report at bugs.python.org>

More information about the New-bugs-announce mailing list