- Issue #21539: Add a *exists_ok* argument to Pathlib.mkdir() to mimic

`mkdir -p` and `os.makedirs()` functionality.  When true, ignore
  FileExistsErrors.  Patch by Berker Peksag.

(With minor cleanups, additional tests, doc tweaks, etc. by Barry)

Also:

* Remove some unused imports in test_pathlib.py reported by pyflakes.
This commit is contained in:
Barry Warsaw 2014-08-05 11:28:12 -04:00
parent 17fd1e1013
commit 7c549c4e64
5 changed files with 78 additions and 9 deletions

View file

@ -1106,14 +1106,21 @@ class Path(PurePath):
fd = self._raw_open(flags, mode)
os.close(fd)
def mkdir(self, mode=0o777, parents=False):
def mkdir(self, mode=0o777, parents=False, exist_ok=False):
if self._closed:
self._raise_closed()
if not parents:
self._accessor.mkdir(self, mode)
try:
self._accessor.mkdir(self, mode)
except FileExistsError:
if not exist_ok or not self.is_dir():
raise
else:
try:
self._accessor.mkdir(self, mode)
except FileExistsError:
if not exist_ok or not self.is_dir():
raise
except OSError as e:
if e.errno != ENOENT:
raise