Is there any to restore files from the recycle bin in python?
Here's the code:
from send2trash import send2trash
file_name = "test.txt"
operation = input("Enter the operation to perform[delete/restore]: ")
if operation == "delete":
send2trash(file_name)
print(f"Successfully deleted {file_name}")
else:
# Code to restore the file from recycle bin.
pass
Here when I type "restore"
in the input()
function, I want to restore my deleted file from the recycle bin.
Is there any way to achieve this in python?
It would be great if anyone could help me out.
EDIT:
Thanks for the answer @Kenivia, but I am facing one small issue:
import winshell
r = list(winshell.recycle_bin()) # this lists the original path of all the all items in the recycling bin
file_name = "C:\\test\\Untitled_1.txt" # This file is located in the recycle bin
index = r.index(file_name) # to determine the index of your file
winshell.undelete(r[index].original_filename())
When I run this code, I get an error: ValueError: 'C:\\test\\Untitled_1.txt' is not in list
. Can you please help me out?