In this article, we will see how to safely delete files and folders using the send2trash module in Python. Using send2trash, we can send files to the Trash or Recycle Bin instead of permanently deleting them. The OS module’s unlink(), remove() and rmdir() functions can be used to delete files or folders. But, these functions delete the files permanently. The operations cannot be undone if there were any accidental deletions performed. This can be prevented using send2trash.
- OS : The OS module in Python provides functions for interacting with the operating system. OS module comes with Python’s Standard Library.
- send2trash : Send2Trash is a small package that sends files to the Trash (or Recycle Bin) natively and on all platforms. To install it type the below command in the terminal.
pip install send2trash
Deleting a file or folder
The send2trash() function accepts the location of the file or folder to be deleted.
The process of deleting a directory is same as above. If the directory contains files or other folders, those are also deleted. A TrashPermissionError exception is raised, in case a file could not be deleted due to permission error or any other unexpected reason.
Deleting specific files in a directory
We can use the os.walk() function to walk through a directory and delete specific files. In the example below, we will delete all ‘.txt’ files in the given directory.
deleted : /Users/tithighosh/Documents/cfile.txt deleted : /Users/tithighosh/Documents/e_also_big_output.txt deleted : /Users/tithighosh/Documents/res.txt deleted : /Users/tithighosh/Documents/tk.txt
Attention geek! Strengthen your foundations with the Python Programming Foundation Course and learn the basics.
To begin with, your interview preparations Enhance your Data Structures concepts with the Python DS Course.
- Downloading files from web using Python
- Rename multiple files using Python
- Check if directory contains files using python
- Uploading files on Google Drive using Python
- Compare two files using Hasing in Python
- Create temporary files and directories using Python-tempfile
- Joining Excel Data from Multiple files using Python Pandas
- Working with wav files in Python using Pydub
- Python program to find files having a particular extension using RegEx
- Deleting Files in HDFS using Python Snakebite
- Get list of files and folders in Google Drive storage using Python
- Creating Files in HDFS using Python Snakebite
- Creating a dataframe using Excel files
- Working with excel files using Pandas
- Creating a dataframe using CSV files
- Python | Writing to an excel file using openpyxl module
- Count frequencies of all elements in array in Python using collections module
- Prefix matching in Python using pytrie module
- Stack and Queue in Python using queue Module
- Testing in Python using doctest module
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to email@example.com. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.