I'm looking for a function to erase all the contents in the folder

Asked 2 years ago, Updated 2 years ago, 105 views

I'm looking for a function to erase all the contents in the folder

I looked it up, but os.rmdir() can only erase empty directories (although I can recursively call and create them myself).

I want to know if there's a function that erases the contents

file python local delete-dictionary

2022-09-21 23:22

1 Answers

shutil module has shutil.rmtree.

The usage is approximately as follows.

import shutil
shutil.rmtree('/path/to/folder')

shutil.rmtree (path, ignore_errors=False, on error=None)

Delete an entire directory tree; path must point to a directory (but not a symbolic link to a directory). If ignore_errors is true, errors resulting from failed removals will be ignored; if false or omitted, such errors are handled by calling a handler specified by onerror or, if that is omitted, they raise an exception.

Clears the entire directory tree. The path must be a directory (not included for symbolic links pointing to the directory). If ignore_errors is true, the error is ignored even if the uninstallation fails.


2022-09-21 23:22

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.