How to Access Recycle-bin with Python, and get rid of the hassle for viewing deleted files directly.

All of our deleted files and folders move to Recycle-Bin temporarily, before they vanish from existence permanently. But have you ever deleted an important file or a folder by mistake and by the moment you realized it, the name of file is long gone from your memory. Traditional Recycle-Bin that comes with our very own Windows or any other OS, only shows you the list of files and folders that you’ve deleted, along with the location from where it was deleted, as a reference but it doesn’t allow you to open any file or folder unless you restore it. Too much hassle just to find one single file, Right?

Image for post

Hassle of viewing file/folder in the recycle-bin

There’s a library in Python, with which you can access the Recycle-Bin and its items easily, through which you can code along to make the right program to find your deleted important file without that much of a hassle.

Start by installing the library “winshell”:

pip install winshell

Winshell library has a module named “recycle_bin()” , which returns a list of objects of recycle-bin items.

In order to print the list of items present in recycle-bin, the code below can be used.

In the above code, the list of objects of recycle-bin items is being iterated over a loop inside the enumerate function.

enumerate() is a python built-in function that returns 2 parameters i.e index and value of a list.

Inside the loop the original_filename() method is called of every object coming form the list.

Original_filename() method returns the path of the file/folder from which it was deleted.


Now comes the part of viewing the file or a folder right from the Recycle-bin.

In order to do this, the index of that file/folder should be noted first. Store its object inside a variable by the index and then undelete it with winshell.undelete() method. Once the file/folder is undeleted, Read its content by using its path, and after that delete it again immediately with **winshell.delete_file() **.

Accessing File:

Accessing File

In the above code:

  • The deleted item object of a file was accessed by index and into variable “obj”.
  • Since Python accepts path containing “\” in it and obj.original_filename() return path with “\”. So that in the properPath variable, path containing the “\” is saved.
  • The selected file is undeleted by winshell.undelete() method, providing the path from obj.original_filename() method [winshell library methods accepts and returns the path containing “\” that’s why the path was given by the original_filename() method].
  • Reading the content of file by opening the

#cli #programming #python #gui #recycle-bin

How to Access Recycle-Bin in Python
1.85 GEEK