首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >在python中从回收站恢复文件

问在python中从回收站恢复文件
EN

Stack Overflow用户
提问于 2021-03-16 15:23:40
回答 1查看 339关注 0票数 2

在python中有没有从回收站恢复文件的方法?

代码如下:

代码语言:javascript
复制
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

在这里,当我在input()函数中键入"restore"时,我想从回收站恢复已删除的文件。

在python中有什么方法可以做到这一点吗?

如果有人能帮我,那就太好了。

编辑:

谢谢你的回答@Kenivia,但我面临着一个小问题:

代码语言:javascript
复制
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())

当我运行这段代码时,我得到一个错误:ValueError: 'C:\\test\\Untitled_1.txt' is not in list。你能帮帮我吗?

EN

回答 1

Stack Overflow用户

发布于 2021-03-16 15:55:12

这将取决于您的操作系统。

Linux

只需将其从回收站文件夹移至原始路径即可。垃圾文件夹的位置因发行版的不同而不同,但这是它通常所在的位置。

这里有一个command line tool,你可以使用它,或者深入研究代码来获得一些想法。

代码语言:javascript
复制
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

在macOS上,您可以执行与在Linux中相同的操作,只是回收站路径为~/.Trash。

代码语言:javascript
复制
import subprocess as sp

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

请注意,macOS将有关文件的信息存储在~/.Trash/.DS_Store中,而Linux将这些信息存储在/home/USERNAME/.local/share/Trash/info/中。如果您不知道文件的原始路径,这会很有用。

Windows

您必须使用winshell。有关详细信息,请参阅this article

代码语言:javascript
复制
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())
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/66650626

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档