Created
November 8, 2024 22:37
-
-
Save heathhenley/c15a0ef60668904eed87ffb29555821f to your computer and use it in GitHub Desktop.
Remove a file or a directory (even if not empty)
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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