![]() |
VOOZH | about |
The send2trash module allows files and folders to be moved to the system's Trash or Recycle Bin instead of being permanently deleted. This provides a safer alternative to functions such as os.remove() and os.rmdir(), as deleted items can be restored if removed accidentally.
Install send2trash using following command:
pip install send2trash
The send2trash() function moves a file to the system's Trash or Recycle Bin instead of permanently deleting it.
Example:
Output
File moved to Trash.
Explanation:
Syntax:
send2trash.send2trash(path)
The send2trash() function supports deleting directories by moving them to the Trash or Recycle Bin. Any files and nested folders inside the directory are moved along with it, providing a safe alternative to permanent deletion.
Example:
Output
Folder moved to Trash.
Explanation:
Syntax:
send2trash.send2trash(folder_path)
A list of file paths can be processed to move multiple files to the Trash at once.
Example:
Output
Files moved to Trash.
Explanation:
Exception handling can be used to manage permission errors and invalid paths during deletion.
Example:
Output
If no error raised: File moved to Trash.
If error raised: Error: Permission denied
Explanation: