在python中有没有从回收站恢复文件的方法?
代码如下:
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,但我面临着一个小问题:
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。你能帮帮我吗?
发布于 2021-03-16 15:55:12
这将取决于您的操作系统。
Linux
只需将其从回收站文件夹移至原始路径即可。垃圾文件夹的位置因发行版的不同而不同,但这是它通常所在的位置。
这里有一个command line tool,你可以使用它,或者深入研究代码来获得一些想法。
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。
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
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())https://stackoverflow.com/questions/66650626
复制相似问题