mirror of
https://github.com/python/cpython.git
synced 2025-10-07 07:31:46 +00:00
- 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:
parent
17fd1e1013
commit
7c549c4e64
5 changed files with 78 additions and 9 deletions
|
@ -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
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue