How to Access Recycle-Bin in Python

Shahzaibceo
Aug 20, 2020 · 5 min read

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?

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 file with open() function with “r” as a parameter for reading purpose, and then file.read() is used to save all the content inside the file, then closed the file with file.close().
  • Once after the content is read successfully, the file is deleted again with winshell.delete_file() method by giving it path by obj.original_fileneme() method.

Accessing Folder:

In the above code:

  • The deleted item object of a folder was accessed by index and into variable “obj”.
  • The “\” path was updated to “\\” path in properPath variable.
  • The selected file is undeleted by winshell.undelete() method, providing the path from obj.original_filename() method.
  • Reading list of file/folders inside our selected folder by using os.listdir method in which the “\\” path is given as a parameter from the properPath variable.

os.listdir() method returns the list of files/folders , of the folder given as parameter.

  • Once after the content is read successfully, the folder is deleted again with winshell.delete_file() method by giving it path by obj.original_fileneme() method.

This way we can view the content of all files/folders inside the recycle-bin without that much of a hassle.

You can now be sure of which file/folder to keep, and which to delete permanently , after viewing what’s inside it.

[ One thing to keep in mind is that winshell.recycle_bin() returns the list of deleted items and the list get shuffled on every execution. Means that your file/folder may be at index 3, but when you’ll re-run the program/code it may got to index 5 now ]

This was all about Accessing Recycle-Bin and deleted items in Python.

There’s a whole program coded on Command line interface in the repository below.

In order to access the CLI program , follow the steps below.

Start by cloning this repository.

$ git clone https://github.com/shahzaibk23/Trash-Visualiser

Once after the cloning is successful, cd into repo folder.

cd Trash-Visualiser/src/CLI/

Then:

python main.py
Working of CLI Program

There’s also a GUI based version available, with which you can get out this hassle in a better visual representation of everything, found in the same repository.

In order to run it , follow the steps below:

Start by cloning the Repository

$ git clone https://github.com/shahzaibk23/Trash-Visualiser

After cloning is successful, cd into repo folder

cd Trash-Visualiser

Then run the launcher

python launcher.py
Windows Popup (Optional)

This may happen that the windows will ask you to create some deleted folders once again, so simply click YES, this way the folders you’d have deleted will be created once again temporarily, but they will be deleted automatically, so no need to Panic.

After a few seconds of processing, Trash-Visualiser main window will be opened.

Main Window of Trash-Visualiser

From this GUI version you can view files and folders inside recycle-bin directly within the GUI. You can also view the files/folders inside the deleted folder as well. File/Folders inside the recycle-bin can be Restores and Deleted permanently with a single button click from the GUI.

More detailed information is found in the Repository of Trash-Visualiser.

Operating of the GUI version

This was all about minimizing the Hassle of Viewing Files/Folders from the Recycle-Bin without restoring at all, simply and efficiently.

The Startup

Get smarter at building your thing. Join The Startup’s +800K followers.

Medium is an open platform where 170 million readers come to find insightful and dynamic thinking. Here, expert and undiscovered voices alike dive into the heart of any topic and bring new ideas to the surface. Learn more

Follow the writers, publications, and topics that matter to you, and you’ll see them on your homepage and in your inbox. Explore

If you have a story to tell, knowledge to share, or a perspective to offer — welcome home. It’s easy and free to post your thinking on any topic. Write on Medium

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store