How to Access Recycle-Bin in Python

How to Access Recycle-Bin in Python

All of our deleted files and folders move to Recycle-Bin temporarily, before they vanish from existence permanently. How to Access Recycle-bin with Python, and get rid of the hassle for viewing deleted files directly.

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

Bootstrap 5 Complete Course with Examples

Bootstrap 5 Tutorial - Bootstrap 5 Crash Course for Beginners

Nest.JS Tutorial for Beginners

Hello Vue 3: A First Look at Vue 3 and the Composition API

Building a simple Applications with Vue 3

Deno Crash Course: Explore Deno and Create a full REST API with Deno

How to Build a Real-time Chat App with Deno and WebSockets

Convert HTML to Markdown Online

HTML entity encoder decoder Online

Python GUI Programming Projects using Tkinter and Python 3

Python GUI Programming Projects using Tkinter and Python 3

Guide to Python Programming Language

Guide to Python Programming Language

Basic GUI Calculator in Python - Python Programming 

Basic GUI Calculator in Python - Python Programming  - This article will take you through the entire process of developing a GUI calculator in Python. If you are reading this article, you…

Python Programming: A Beginner’s Guide

Python is an interpreted, high-level, powerful general-purpose programming language. You may ask, Python’s a snake right? and Why is this programming language named after it?

Python Hello World Program - Create & Run Your First Python Program in PyCharm

Python Hello World Program - Your first step towards Python world. Learn how to create the Hello World Python program in PyCharm.