Skip to content

Commit d9e006b

Browse files
rbumiss-islington
authored andcommitted
bpo-33123: pathlib: Add missing_ok parameter to Path.unlink (pythonGH-6191)
Similarly to how several pathlib file creation functions have an "exists_ok" parameter, we should introduce "missing_ok" that makes removal functions not raise an exception when a file or directory is already absent. IMHO, this should cover Path.unlink and Path.rmdir. Note, Path.resolve() has a "strict" parameter since 3.6 that does the same thing. Naming this of this new parameter tries to be consistent with the "exists_ok" parameter as that is more explicit about what it does (as opposed to "strict"). https://bugs.python.org/issue33123
1 parent 1a2dd82 commit d9e006b

File tree

4 files changed

+23
-3
lines changed

4 files changed

+23
-3
lines changed

Doc/library/pathlib.rst

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1048,11 +1048,20 @@ call fails (for example because the path doesn't exist).
10481048
otherwise :exc:`FileExistsError` is raised.
10491049

10501050

1051-
.. method:: Path.unlink()
1051+
.. method:: Path.unlink(missing_ok=False)
10521052

10531053
Remove this file or symbolic link. If the path points to a directory,
10541054
use :func:`Path.rmdir` instead.
10551055

1056+
If *missing_ok* is false (the default), :exc:`FileNotFoundError` is
1057+
raised if the path does not exist.
1058+
1059+
If *missing_ok* is true, :exc:`FileNotFoundError` exceptions will be
1060+
ignored (same behavior as the POSIX ``rm -f`` command).
1061+
1062+
.. versionchanged:: 3.8
1063+
The *missing_ok* parameter was added.
1064+
10561065

10571066
.. method:: Path.link_to(target)
10581067

Lib/pathlib.py

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1279,14 +1279,18 @@ def lchmod(self, mode):
12791279
self._raise_closed()
12801280
self._accessor.lchmod(self, mode)
12811281

1282-
def unlink(self):
1282+
def unlink(self, missing_ok=False):
12831283
"""
12841284
Remove this file or link.
12851285
If the path is a directory, use rmdir() instead.
12861286
"""
12871287
if self._closed:
12881288
self._raise_closed()
1289-
self._accessor.unlink(self)
1289+
try:
1290+
self._accessor.unlink(self)
1291+
except FileNotFoundError:
1292+
if not missing_ok:
1293+
raise
12901294

12911295
def rmdir(self):
12921296
"""

Lib/test/test_pathlib.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1635,6 +1635,11 @@ def test_unlink(self):
16351635
self.assertFileNotFound(p.stat)
16361636
self.assertFileNotFound(p.unlink)
16371637

1638+
def test_unlink_missing_ok(self):
1639+
p = self.cls(BASE) / 'fileAAA'
1640+
self.assertFileNotFound(p.unlink)
1641+
p.unlink(missing_ok=True)
1642+
16381643
def test_rmdir(self):
16391644
p = self.cls(BASE) / 'dirA'
16401645
for q in p.iterdir():
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
:class:`pathlib.Path.unlink` now accepts a *missing_ok* parameter to avoid a
2+
:exc:`FileNotFoundError` from being raised. Patch by Robert Buchholz.

0 commit comments

Comments
 (0)