Skip to content

Commit 613cdb2

Browse files
ignore NotADirectoryError on missing_ok = True
Semantically equivalent to FileNotFoundError - the file to be unlinked does not exist
1 parent 6286d80 commit 613cdb2

File tree

5 files changed

+22
-3
lines changed

5 files changed

+22
-3
lines changed

Doc/library/pathlib.rst

+7-2
Original file line numberDiff line numberDiff line change
@@ -1627,10 +1627,15 @@ example because the path doesn't exist).
16271627
If *missing_ok* is false (the default), this method propagates any
16281628
:exc:`OSError` from the operating system, including :exc:`FileNotFoundError`.
16291629

1630-
If *missing_ok* is true, :exc:`FileNotFoundError` exceptions will be
1631-
ignored (same behavior as the POSIX ``rm -f`` command), any other
1630+
If *missing_ok* is true, this shows similar behavior to the POSIX ``rm -f``
1631+
command and any :exc:`FileNotFoundError` or :exc:`NotADirectoryError`
1632+
exceptions will be ignored. This means that the file does not exist after
1633+
execution, but cannot guarantee that the file did exist before. Any other
16321634
:exc:`OSError` which is encountered will continue to be propogated.
16331635

1636+
.. versionchanged:: 3.??
1637+
The *missing_ok* parameter will also ignore :exc:`NotADirectoryError`
1638+
16341639
.. versionchanged:: 3.8
16351640
The *missing_ok* parameter was added.
16361641

Lib/pathlib/_local.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -793,7 +793,7 @@ def unlink(self, missing_ok=False):
793793
"""
794794
try:
795795
os.unlink(self)
796-
except FileNotFoundError:
796+
except (FileNotFoundError, NotADirectoryError):
797797
if not missing_ok:
798798
raise
799799

Lib/test/test_pathlib/test_pathlib.py

+7
Original file line numberDiff line numberDiff line change
@@ -775,6 +775,13 @@ def test_unlink_missing_ok(self):
775775
self.assertFileNotFound(p.unlink)
776776
p.unlink(missing_ok=True)
777777

778+
def test_unlink_missing_ok_intermediate_file(self):
779+
p = self.cls(self.base) / 'fileAAA'
780+
p.touch()
781+
p = p / 'fileBBB'
782+
self.assertNotADirectory(p.unlink)
783+
p.unlink(missing_ok=True)
784+
778785
def test_rmdir(self):
779786
p = self.cls(self.base) / 'dirA'
780787
for q in p.iterdir():

Lib/test/test_pathlib/test_pathlib_abc.py

+5
Original file line numberDiff line numberDiff line change
@@ -1591,6 +1591,11 @@ def assertFileNotFound(self, func, *args, **kwargs):
15911591
func(*args, **kwargs)
15921592
self.assertEqual(cm.exception.errno, errno.ENOENT)
15931593

1594+
def assertNotADirectory(self, func, *args, **kwargs):
1595+
with self.assertRaises(NotADirectoryError) as cm:
1596+
func(*args, **kwargs)
1597+
self.assertEqual(cm.exception.errno, errno.ENOTDIR)
1598+
15941599
def assertEqualNormCase(self, path_a, path_b):
15951600
normcase = self.parser.normcase
15961601
self.assertEqual(normcase(path_a), normcase(path_b))
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
:meth:`pathlib.Path.unlink` will also ignore any :exc:`NotADirectoryError`
2+
if *missing_ok* is true.

0 commit comments

Comments
 (0)