Skip to content

Commit

Permalink
Add follow_symlink argument to rmdir() (#141)
Browse files Browse the repository at this point in the history
  • Loading branch information
hagenw authored Jan 22, 2024
1 parent cebd0a3 commit 2ec395a
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 1 deletion.
9 changes: 8 additions & 1 deletion audeer/core/io.py
Original file line number Diff line number Diff line change
Expand Up @@ -970,6 +970,7 @@ def replace_file_extension(
def rmdir(
path: typing.Union[str, bytes],
*paths: typing.Sequence[typing.Union[str, bytes]],
follow_symlink: bool = True,
):
"""Remove directory.
Expand All @@ -982,9 +983,15 @@ def rmdir(
*paths: additional arguments
to be joined with ``path``
by :func:`os.path.join`
follow_symlink: if ``True``
and path is a symbolic link,
the link and the folder it points to
will be removed
Raises:
NotADirectoryError: if path is not a directory
OSError: if ``follow_symlink`` is ``False``
and ``path`` is a symbolic link
Examples:
>>> _ = mkdir("path1", "path2", "path3")
Expand All @@ -995,7 +1002,7 @@ def rmdir(
[]
"""
path = safe_path(path, *paths)
path = safe_path(path, *paths, follow_symlink=follow_symlink)
if os.path.exists(path):
shutil.rmtree(path)

Expand Down
11 changes: 11 additions & 0 deletions tests/test_io.py
Original file line number Diff line number Diff line change
Expand Up @@ -1517,6 +1517,17 @@ def test_rmdir(tmpdir):
audeer.rmdir("folder")
assert not os.path.exists(path)
os.chdir(current_path)
# Symbolic link
path = audeer.mkdir(tmpdir, "folder")
link = os.path.join(tmpdir, "link")
os.symlink(path, link)
with pytest.raises(OSError, match="symbolic link"):
audeer.rmdir(link, follow_symlink=False)
assert os.path.exists(link)
assert os.path.exists(path)
audeer.rmdir(link, follow_symlink=True)
assert not os.path.exists(link)
assert not os.path.exists(path)


def test_touch(tmpdir):
Expand Down

0 comments on commit 2ec395a

Please sign in to comment.