DEV Community

Tommi k Vincent
Tommi k Vincent

Posted on

safe Deletes with send2trash Module in python.

First, install the send2trash module by running pip install send2trash in your terminal, Using send2trash is much safer than Python's regular delete functions, because it will send folders and files to your computer's trash or recycle bin instead of deleting them permanently.
After you have installed send2trash, enter the following into the interactive shell, start by running python3 to launch the interactive shell then type the following code below:

Image description
Refresh your folder area and you will see the file has disappeared, it is still located in the recycle bin.

NB:
In general, you should always use the send2trash.send2trash() function to delete files and folders. But while sending files to the recycle bin lets you recover them later, it will not free up disk space like permanently deleting them does. If you want your program to free up disk space, use the os and shutil functions for deleting files and folders. Note that the send2trash()function can only send files to the recycle bin; it cannot pull files out of it.

Top comments (0)