Permanently Deleting Files and
Folders
• One can delete a single file or a single empty folder with
functions in the os module, whereas for a non-empty folder
and all of its contents, we use the shutil module.
• Calling os.unlink(path) will delete a file at the specified path.
• Calling os.rmdir(path) will delete a folder at the specified path. The
folder must be empty of any files or folders.
• Calling shutil.rmtree(path) will remove the folder at the specified
path and all files and folders it contains will be deleted too.
• Be very careful in using these functions. It is a good idea to first
print out the content intended to be deleted before calling
these delete function on the content to avoid mistakenly
deleting important files.
8