If you want to delete files in Python, you can use functions from Python’s built-in OS module.
However, if it’s not an empty folder, you must use the rmtree() function from the shutil module.
These functions, however, will fail if the file or directory to be removed does not exist. We can prevent this by using os.path.isfile() and os.path.isdir() to check for the existence of files and directories before deleting them:
For more such Python tricks, follow MarsDevs blogs.