Skip to content

Commit 2ec395a

Browse files
authored
Add follow_symlink argument to rmdir() (#141)
1 parent cebd0a3 commit 2ec395a

File tree

2 files changed

+19
-1
lines changed

2 files changed

+19
-1
lines changed

audeer/core/io.py

+8-1
Original file line numberDiff line numberDiff line change
@@ -970,6 +970,7 @@ def replace_file_extension(
970970
def rmdir(
971971
path: typing.Union[str, bytes],
972972
*paths: typing.Sequence[typing.Union[str, bytes]],
973+
follow_symlink: bool = True,
973974
):
974975
"""Remove directory.
975976
@@ -982,9 +983,15 @@ def rmdir(
982983
*paths: additional arguments
983984
to be joined with ``path``
984985
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
985990
986991
Raises:
987992
NotADirectoryError: if path is not a directory
993+
OSError: if ``follow_symlink`` is ``False``
994+
and ``path`` is a symbolic link
988995
989996
Examples:
990997
>>> _ = mkdir("path1", "path2", "path3")
@@ -995,7 +1002,7 @@ def rmdir(
9951002
[]
9961003
9971004
"""
998-
path = safe_path(path, *paths)
1005+
path = safe_path(path, *paths, follow_symlink=follow_symlink)
9991006
if os.path.exists(path):
10001007
shutil.rmtree(path)
10011008

tests/test_io.py

+11
Original file line numberDiff line numberDiff line change
@@ -1517,6 +1517,17 @@ def test_rmdir(tmpdir):
15171517
audeer.rmdir("folder")
15181518
assert not os.path.exists(path)
15191519
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)
15201531

15211532

15221533
def test_touch(tmpdir):

0 commit comments

Comments
 (0)