From 4277aab33b229150228671918d2a1cf91e13b6ea Mon Sep 17 00:00:00 2001 From: Robert Buchholz Date: Thu, 22 Mar 2018 18:57:00 +0100 Subject: [PATCH 1/2] pathlib: Add missing_ok parameter to Path.unlink Fixes bpo-33123 --- Doc/library/pathlib.rst | 11 ++++++++++- Lib/pathlib.py | 8 ++++++-- Lib/test/test_pathlib.py | 5 +++++ .../Library/2018-03-22-19-13-19.bpo-33123._Y5ooE.rst | 2 ++ 4 files changed, 23 insertions(+), 3 deletions(-) create mode 100644 Misc/NEWS.d/next/Library/2018-03-22-19-13-19.bpo-33123._Y5ooE.rst diff --git a/Doc/library/pathlib.rst b/Doc/library/pathlib.rst index 3d28a1bea30315..1ba6eb0db6dfa1 100644 --- a/Doc/library/pathlib.rst +++ b/Doc/library/pathlib.rst @@ -1033,11 +1033,20 @@ call fails (for example because the path doesn't exist): otherwise :exc:`FileExistsError` is raised. -.. method:: Path.unlink() +.. method:: Path.unlink(missing_ok=False) Remove this file or symbolic link. If the path points to a directory, use :func:`Path.rmdir` instead. + If *missing_ok* is false (the default), :exc:`FileNotFoundError` is + raised if the path does not exist. + + If *missing_ok* is true, :exc:`FileNotFoundError` exceptions will be + ignored (same behavior as the POSIX ``rm -f`` command). + + .. versionchanged:: 3.8 + The *missing_ok* parameter was added. + .. method:: Path.write_bytes(data) diff --git a/Lib/pathlib.py b/Lib/pathlib.py index 8431c29c1d6516..f42f63ab76ba5a 100644 --- a/Lib/pathlib.py +++ b/Lib/pathlib.py @@ -1256,14 +1256,18 @@ def lchmod(self, mode): self._raise_closed() self._accessor.lchmod(self, mode) - def unlink(self): + def unlink(self, missing_ok=False): """ Remove this file or link. If the path is a directory, use rmdir() instead. """ if self._closed: self._raise_closed() - self._accessor.unlink(self) + try: + self._accessor.unlink(self) + except FileNotFoundError: + if not missing_ok: + raise def rmdir(self): """ diff --git a/Lib/test/test_pathlib.py b/Lib/test/test_pathlib.py index 53215550b4354e..b0804f07ab0ad5 100644 --- a/Lib/test/test_pathlib.py +++ b/Lib/test/test_pathlib.py @@ -1627,6 +1627,11 @@ def test_unlink(self): self.assertFileNotFound(p.stat) self.assertFileNotFound(p.unlink) + def test_unlink_missing_ok(self): + p = self.cls(BASE) / 'fileAAA' + self.assertFileNotFound(p.unlink) + p.unlink(missing_ok=True) + def test_rmdir(self): p = self.cls(BASE) / 'dirA' for q in p.iterdir(): diff --git a/Misc/NEWS.d/next/Library/2018-03-22-19-13-19.bpo-33123._Y5ooE.rst b/Misc/NEWS.d/next/Library/2018-03-22-19-13-19.bpo-33123._Y5ooE.rst new file mode 100644 index 00000000000000..2c22add048b662 --- /dev/null +++ b/Misc/NEWS.d/next/Library/2018-03-22-19-13-19.bpo-33123._Y5ooE.rst @@ -0,0 +1,2 @@ +:class:`pathlib.Path.unlink` now accepts a +missing_ok* parameter to avoid a +:exc:`FileNotFoundError` from being raised. Patch by Robert Buchholz. From c7ce026a95cf6272699b61631e64d5c49a1e4370 Mon Sep 17 00:00:00 2001 From: Antoine Pitrou Date: Wed, 15 May 2019 23:42:55 +0200 Subject: [PATCH 2/2] Fix markup in NEWS file --- .../next/Library/2018-03-22-19-13-19.bpo-33123._Y5ooE.rst | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Misc/NEWS.d/next/Library/2018-03-22-19-13-19.bpo-33123._Y5ooE.rst b/Misc/NEWS.d/next/Library/2018-03-22-19-13-19.bpo-33123._Y5ooE.rst index 2c22add048b662..8803ca84313122 100644 --- a/Misc/NEWS.d/next/Library/2018-03-22-19-13-19.bpo-33123._Y5ooE.rst +++ b/Misc/NEWS.d/next/Library/2018-03-22-19-13-19.bpo-33123._Y5ooE.rst @@ -1,2 +1,2 @@ -:class:`pathlib.Path.unlink` now accepts a +missing_ok* parameter to avoid a +:class:`pathlib.Path.unlink` now accepts a *missing_ok* parameter to avoid a :exc:`FileNotFoundError` from being raised. Patch by Robert Buchholz.