Commit 2ec395a 1 parent cebd0a3 commit 2ec395a Copy full SHA for 2ec395a
File tree 2 files changed +19
-1
lines changed
2 files changed +19
-1
lines changed Original file line number Diff line number Diff line change @@ -970,6 +970,7 @@ def replace_file_extension(
970
970
def rmdir (
971
971
path : typing .Union [str , bytes ],
972
972
* paths : typing .Sequence [typing .Union [str , bytes ]],
973
+ follow_symlink : bool = True ,
973
974
):
974
975
"""Remove directory.
975
976
@@ -982,9 +983,15 @@ def rmdir(
982
983
*paths: additional arguments
983
984
to be joined with ``path``
984
985
by :func:`os.path.join`
986
+ follow_symlink: if ``True``
987
+ and path is a symbolic link,
988
+ the link and the folder it points to
989
+ will be removed
985
990
986
991
Raises:
987
992
NotADirectoryError: if path is not a directory
993
+ OSError: if ``follow_symlink`` is ``False``
994
+ and ``path`` is a symbolic link
988
995
989
996
Examples:
990
997
>>> _ = mkdir("path1", "path2", "path3")
@@ -995,7 +1002,7 @@ def rmdir(
995
1002
[]
996
1003
997
1004
"""
998
- path = safe_path (path , * paths )
1005
+ path = safe_path (path , * paths , follow_symlink = follow_symlink )
999
1006
if os .path .exists (path ):
1000
1007
shutil .rmtree (path )
1001
1008
Original file line number Diff line number Diff line change @@ -1517,6 +1517,17 @@ def test_rmdir(tmpdir):
1517
1517
audeer .rmdir ("folder" )
1518
1518
assert not os .path .exists (path )
1519
1519
os .chdir (current_path )
1520
+ # Symbolic link
1521
+ path = audeer .mkdir (tmpdir , "folder" )
1522
+ link = os .path .join (tmpdir , "link" )
1523
+ os .symlink (path , link )
1524
+ with pytest .raises (OSError , match = "symbolic link" ):
1525
+ audeer .rmdir (link , follow_symlink = False )
1526
+ assert os .path .exists (link )
1527
+ assert os .path .exists (path )
1528
+ audeer .rmdir (link , follow_symlink = True )
1529
+ assert not os .path .exists (link )
1530
+ assert not os .path .exists (path )
1520
1531
1521
1532
1522
1533
def test_touch (tmpdir ):
You can’t perform that action at this time.
0 commit comments