Skip to content

Inconsistent behaviour in rmdir #559

Description

@TLCFEM

This line declares default value recursive=True.

def rmdir(self, recursive: bool = True) -> None: # fixme: non-standard
"""
Remove this directory.
Warning
-------
This method is non-standard compared to pathlib.Path.rmdir(),
as it supports a `recursive` parameter to remove non-empty
directories and defaults to recursive deletion.
This behavior is likely to change in future releases once
`.delete()` is introduced.
"""
if not self.is_dir():
raise NotADirectoryError(str(self))
if not recursive and next(self.iterdir()): # type: ignore[arg-type]
raise OSError(f"Not recursive and directory not empty: {self}")
self.fs.rm(self.path, recursive=recursive)

However, here the behavior is different.

def rmdir(self, recursive: bool = UNSET_DEFAULT) -> None:
if recursive is UNSET_DEFAULT or not recursive:
return super().rmdir()
else:
shutil.rmtree(self)

Hence when I call rmdir on a non-empty local folder, it throws.

from upath import UPath
from os import makedirs

makedirs('test/folder', exist_ok=True)
UPath('test').rmdir()
OSError: [Errno 39] Directory not empty: 'test'

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions