Wednesday, July 3, 2024
HomeLanguagesPythonHow to Delete files in Python using send2trash module?

How to Delete files in Python using send2trash module?

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.

Modules required

  • 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.

Python3




import send2trash
  
send2trash.send2trash("/location/to/file")


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.

Python3




import os
import send2trash
  
# walking through the directory
for folder, subfolders, files in os.walk('/Users/tithighosh/Documents'):
      
    for file in files:
        
        # checking if file is
        # of .txt type
        if file.endswith('.txt'):
            path = os.path.join(folder, file)
              
            # printing the path of the file
            # to be deleted
            print('deleted : ', path )
              
            # deleting the file
            send2trash.send2trash(path)


Output :

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

Shaida Kate Naidoo
am passionate about learning the latest technologies available to developers in either a Front End or Back End capacity. I enjoy creating applications that are well designed and responsive, in addition to being user friendly. I thrive in fast paced environments. With a diverse educational and work experience background, I excel at collaborating with teams both local and international. A versatile developer with interests in Software Development and Software Engineering. I consider myself to be adaptable and a self motivated learner. I am interested in new programming technologies, and continuous self improvement.
RELATED ARTICLES

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Most Popular

Recent Comments