Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add follow_symlink argument to rmdir() #141

Merged
merged 2 commits into from
Jan 22, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 @@ -1466,6 +1466,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