Python Send2Trash


This tutorial covers the use of the Python Send2Trash library.

Ever wondered how to delete a file(s) without that deletion being permanent? Maybe you’re in your test phase and don’t want to delete anything permanently by accident. Remember, these deletions can’t be undone in any way. This kind of situation can come up often during File handling in Python.

Python Send2Trash is a library that will help you get around this that. Install this Library first, either through the command prompt or an IDE. Refer to this guide if you don’t know how to do this. If you can execute the following command in your IDE, you have installed it successfully.

import send2trash

Safe Deleting with send2trash()

We just need a single function from the Send2Trash Library called send2trash().

There are several things to keep in mind about this function. For one, it can delete both files and even entire directories. Secondly, in case you are deleting a file, make sure to include it’s file extension, else it will think it’s a folder and delete that instead.

The below format is used for deleting files:

import send2trash

send2trash.send2trash.('C:\\Users\CodersLegacy\testfile.txt')

The below format is used for deleting directories:

import send2trash

send2trash.send2trash.('C:\\Users\CodersLegacy\testdirectory')

This marks the end of the Python Send2Trash article. Any suggestions or contributions for CodersLegacy are more than welcome. Any relevant questions can be directed below to the comments section.

Subscribe
Notify of
guest
0 Comments
Inline Feedbacks
View all comments