4

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?

Lenovo 360
  • 569
  • 1
  • 7
  • 27

2 Answers2

4

It would depend on your operating system.

Linux

it's as simple as moving it from the trash folder to the original path. The location of the trash folder differs from distro to distro, but this is where it typically is.

There is a command line tool that you can use, or dig through the code to get some ideas.

import subprocess as sp # here subprocess is just used to run the command, you can also use os.system but that is discouraged

sp.run(['mv','/home/USERNAME/.local/share/Trash/files/test.txt', '/ORIGINAL/PATH/')

macOS

On macOS, you do the same thing as you do in Linux, except the trash path is ~/.Trash

import subprocess as sp

sp.run(['mv','~/.Trash/test.txt', '/ORIGINAL/PATH/')

Note that macOS stores information about the files at ~/.Trash/.DS_Store, where Linux stores them at /home/USERNAME/.local/share/Trash/info/. This can be useful if you don't know the original path of the files.

Windows

you have to use winshell. See this article for more details

import winshell 

r = list(winshell.recycle_bin())  # this lists the original path of all the all items in the recycling bin
index = r.index("C:\ORIGINAL\PATH\test.txt") # to determine the index of your file

winshell.undelete(r[index].original_filename())
Kenivia
  • 384
  • 1
  • 13
  • Hi, thanks for your answer, but when I use `r.index(file)`, python gives me an error `ValueError: 'file' is not in list`, though the path of the file I have given is right. Is there any way to fix this? – Lenovo 360 Mar 16 '21 at 08:17
  • You might have used `r.index("file")` instead of `r.index(file)`, if not, please print the list `r` and look for the path of your text file. – Kenivia Mar 16 '21 at 08:20
  • I tried printing the list, and the file path in the list seems to be the same as path I have given. – Lenovo 360 Mar 16 '21 at 08:22
  • the fact that the error message mentions `'file'` means that you put in a string - `"file"` instead of a variable called `file`. Can you just double check that you passed the right thing into `r.index()`? – Kenivia Mar 16 '21 at 08:26
  • Can you please show me your entire code, either by editing the question or sending it in a comment? – Kenivia Mar 16 '21 at 08:31
  • I used something like `index = r.index(x)`, where x is a variable that stores the path of the file. – Lenovo 360 Mar 16 '21 at 08:31
  • Let us [continue this discussion in chat](https://chat.stackoverflow.com/rooms/229969/discussion-between-kenivia-and-lenovo-360). – Kenivia Mar 16 '21 at 08:36
  • 1
    @Kevinia: Hey, I know I'm late, but I think I have figured out the problem (Windows). The reason why `r.index(file)` doesn't work is each item in `r` is an instance of `ShellRecycledItem()`, while `file` is just a string. The `ShellRecycledItem()` class has a method called `original_filename()` which we can use to get the actual name of the file. So, instead of `r = list(winshell.recycle_bin())`, we can do something like `r = [name.original_filename() for name in list(winshell.recycle_bin())]` to make `r.index(file)` work. Thank you so much for your help! Upvoted + Accepted. – Lenovo 360 Aug 07 '22 at 10:37
0

Google Colab (you are the root user)

Import the shell utility for Python:

import shutil

Move the file from trash to a selected destination:

shutil.move('/root/.local/share/Trash/files/<deleted-file>', '<destination-path>')
Amir Dagan
  • 11
  • 2