@@ -1496,7 +1496,7 @@ def iterdir(self):
14961496 if path in self ._files :
14971497 raise NotADirectoryError (errno .ENOTDIR , "Not a directory" , path )
14981498 elif path in self ._directories :
1499- return ( self / name for name in self ._directories [path ])
1499+ return iter ([ self / name for name in self ._directories [path ] ])
15001500 else :
15011501 raise FileNotFoundError (errno .ENOENT , "File not found" , path )
15021502
@@ -1517,6 +1517,37 @@ def mkdir(self, mode=0o777, parents=False, exist_ok=False):
15171517 self .parent .mkdir (parents = True , exist_ok = True )
15181518 self .mkdir (mode , parents = False , exist_ok = exist_ok )
15191519
1520+ def unlink (self , missing_ok = False ):
1521+ path_obj = self .parent .resolve (strict = True ) / self .name
1522+ path = str (path_obj )
1523+ name = path_obj .name
1524+ parent = str (path_obj .parent )
1525+ if path in self ._directories :
1526+ raise IsADirectoryError (errno .EISDIR , "Is a directory" , path )
1527+ elif path in self ._files :
1528+ self ._directories [parent ].remove (name )
1529+ del self ._files [path ]
1530+ elif path in self ._symlinks :
1531+ self ._directories [parent ].remove (name )
1532+ del self ._symlinks [path ]
1533+ elif not missing_ok :
1534+ raise FileNotFoundError (errno .ENOENT , "File not found" , path )
1535+
1536+ def rmdir (self ):
1537+ path_obj = self .parent .resolve (strict = True ) / self .name
1538+ path = str (path_obj )
1539+ if path in self ._files or path in self ._symlinks :
1540+ raise NotADirectoryError (errno .ENOTDIR , "Not a directory" , path )
1541+ elif path not in self ._directories :
1542+ raise FileNotFoundError (errno .ENOENT , "File not found" , path )
1543+ elif self ._directories [path ]:
1544+ raise OSError (errno .ENOTEMPTY , "Directory not empty" , path )
1545+ else :
1546+ name = path_obj .name
1547+ parent = str (path_obj .parent )
1548+ self ._directories [parent ].remove (name )
1549+ del self ._directories [path ]
1550+
15201551
15211552class DummyPathTest (DummyPurePathTest ):
15221553 """Tests for PathBase methods that use stat(), open() and iterdir()."""
@@ -2400,6 +2431,25 @@ def test_complex_symlinks_relative(self):
24002431 def test_complex_symlinks_relative_dot_dot (self ):
24012432 self ._check_complex_symlinks (self .parser .join ('dirA' , '..' ))
24022433
2434+ def test_unlink (self ):
2435+ p = self .cls (self .base ) / 'fileA'
2436+ p .unlink ()
2437+ self .assertFileNotFound (p .stat )
2438+ self .assertFileNotFound (p .unlink )
2439+
2440+ def test_unlink_missing_ok (self ):
2441+ p = self .cls (self .base ) / 'fileAAA'
2442+ self .assertFileNotFound (p .unlink )
2443+ p .unlink (missing_ok = True )
2444+
2445+ def test_rmdir (self ):
2446+ p = self .cls (self .base ) / 'dirA'
2447+ for q in p .iterdir ():
2448+ q .unlink ()
2449+ p .rmdir ()
2450+ self .assertFileNotFound (p .stat )
2451+ self .assertFileNotFound (p .unlink )
2452+
24032453 def setUpWalk (self ):
24042454 # Build:
24052455 # TESTFN/
0 commit comments