Movatterモバイル変換


[0]ホーム

URL:


Open In App
Next Article:
How to Delete Only Empty Folders in Python
Next article icon

In this article, we will see how to safely delete files and folders using thesend2trash module in Python. Usingsend2trash, we can send files to the Trash or Recycle Bin instead of permanently deleting them. TheOS module'sunlink(),remove() andrmdir() 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 usingsend2trash.

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

Thesend2trash() function accepts the location of the file or folder to be deleted.

Python3
importsend2trashsend2trash.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. ATrashPermissionError 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 theos.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
importosimportsend2trash# walking through the directoryforfolder,subfolders,filesinos.walk('/Users/tithighosh/Documents'):forfileinfiles:# checking if file is# of .txt typeiffile.endswith('.txt'):path=os.path.join(folder,file)# printing the path of the file# to be deletedprint('deleted : ',path)# deleting the filesend2trash.send2trash(path)

Output :

deleted :  /Users/tithighosh/Documents/cfile.txtdeleted :  /Users/tithighosh/Documents/e_also_big_output.txtdeleted :  /Users/tithighosh/Documents/res.txtdeleted :  /Users/tithighosh/Documents/tk.txt

Similar Reads

We use cookies to ensure you have the best browsing experience on our website. By using our site, you acknowledge that you have read and understood ourCookie Policy &Privacy Policy
Lightbox
Improvement
Suggest Changes
Help us improve. Share your suggestions to enhance the article. Contribute your expertise and make a difference in the GeeksforGeeks portal.
geeksforgeeks-suggest-icon
Create Improvement
Enhance the article with your expertise. Contribute to the GeeksforGeeks community and help create better learning resources for all.
geeksforgeeks-improvement-icon
Suggest Changes
min 4 words, max Words Limit:1000

Thank You!

Your suggestions are valuable to us.

What kind of Experience do you want to share?

Interview Experiences
Admission Experiences
Career Journeys
Work Experiences
Campus Experiences
Competitive Exam Experiences

[8]ページ先頭

©2009-2025 Movatter.jp