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?
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() *.
In the above code:
Python GUI Programming Projects using Tkinter and Python 3
Guide to Python Programming Language
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 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 - Your first step towards Python world. Learn how to create the Hello World Python program in PyCharm.