Delete files using send2trash module in Python

In this tutorial let us see how to delete files or folders by using the send2trash module in Python.

Python provides other modules like os.unlink, os.remove, and os.rmdir to delete and all of these modules delete the file permanently. However, these methods are not safe, as any small mistake can lead to a great loss of resources.

We can save the data by using the send2trash module. This module can either permanently delete it or move the file to the recycle bin. During the deletion process, if anything done wrong, we can undo that by using restore in the recycle bin.

Installing Send2trash module:

Send2trash is not an inbuilt module so we need to install it.

This can be done through the line.

pip install send2trash

Note: This tutorial is specific to windows computers. With some small changes, you can use this with LINUX-based or Mac-based systems.

Application of Send2trash module:

Now that we have installed the module we are ready to use this.

To use this module, all you have to do is import the send2trash module. Then use the send2trash() function that is a part of this module.

Example: Let us delete a with a filename as intro.txt

import send2trash as stt




The file article will be moved to the recycle bin.

When we run the above code the intro.txt file will be moved to the recycle bin

Now let us see how to delete a directory.

Deleting a directory is very much similar to deleting a file.


import send2trash as stt




The folder article will be moved to the recycle bin.

When we run the above code the article directory will be moved to the recycle bin.


With this note, I want to end this tutorial Thank you.

Also read: How to delete a file in Python with examples

Leave a Reply

Your email address will not be published. Required fields are marked *