Skip to content

Instantly share code, notes, and snippets.

@heathhenley
Created November 8, 2024 22:37
Show Gist options
  • Save heathhenley/c15a0ef60668904eed87ffb29555821f to your computer and use it in GitHub Desktop.
Save heathhenley/c15a0ef60668904eed87ffb29555821f to your computer and use it in GitHub Desktop.
Remove a file or a directory (even if not empty)
def remove(path: pathlib.Path):
""" Remove a file or non-empty directory.
Unlink only works on files, and rmdir requires the directory to be empty
"""
if not path.is_dir():
path.unlink()
return
for child in path.iterdir():
remove(child)
path.rmdir()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment