使用Python恢复Windows、Linux、MacOS回收站中的文件和目录

一、使用Python恢复Windows回收站中的文件和目录

python 复制代码
import os
import platform

def put_back_trash():

    # 获取操作系统类型
    os_type = platform.system()

    if os_type == "Windows":  # Windows

        from winshell import recycle_bin

        for item in recycle_bin():
            winshell.undelete(item.original_filename())
        
    else:
        print("Unsupported operating system.")

put_back_trash()

二、使用Python恢复Linux回收站中的文件和目录

python 复制代码
import os
import platform
import shutil
import urllib.parse


def get_linux_original_path(filename):
    """
    获取回收站中文件的原始路径
    """

    trash_info_file = filename + '.trashinfo'

    trash_info_path = os.path.join(os.path.expanduser('~'), '.local/share/Trash/info', trash_info_file)

    with open(trash_info_path) as f:

        for line in f:

            if line.startswith('Path='):
                return urllib.parse.unquote(line[5:].strip())
    return None

def restore_linux_file(filename):
    """
    将文件从回收站移动到原始位置
    """

    original_path = get_linux_original_path(filename)

    if original_path is None:

        print(f'Error: Could not find original path for {filename}')
        return

    if os.path.exists(original_path):

        print(f'Error: File {original_path} already exists')
        return

    trash_file_path = os.path.join(os.path.expanduser('~'), '.local/share/Trash/files', filename)

    shutil.move(trash_file_path, original_path)

    print(f'Successfully restored {filename} to {original_path}')

def restore_linux_all_files():

    recycle_bin_path = os.path.expanduser("~/.local/share/Trash/files")

    for filename in os.listdir(recycle_bin_path):
        restore_linux_file(filename)

def put_back_trash():
    # 获取操作系统类型
    os_type = platform.system()

    if os_type == "Linux":  # Linux

        restore_linux_all_files()

    else:
        print("Unsupported operating system.")

put_back_trash()

三、使用Python恢复MacOS回收站中的文件和目录

python 复制代码
import os
import platform
import shutil
import urllib.parse


def put_back_trash():
    # 获取操作系统类型
    os_type = platform.system()

    if os_type == "Darwin":  # macOS

        def generate_applescript():

            script = '''tell application "Finder"
        	activate
        	set file_count to count of (trash's items)
        	-- log file_count
        	repeat file_count times
        		recoverMyFile() of me
        	end repeat
        end tell

        on recoverMyFile()
        	tell application "System Events"
        		set frontmost of process "Finder" to true
        		tell application "Finder"
        			open trash
        			select the first item of front window
        		end tell
        		tell process "Finder"
        			key code 51 using command down
        			delay 2 -- Yes, it's stupid, but necessary :(
        		end tell
        	end tell
        end recoverMyFile'''

            with open('apple.script', 'w') as f:
                f.write(script)

        def recover_macOS_all_files():

            generate_applescript()

            os.system("osascript apple.script")

        recover_macOS_all_files()

    else:
        print("Unsupported operating system.")

put_back_trash()
相关推荐
啊阿狸不会拉杆5 分钟前
《机器学习导论》第 9 章-决策树
人工智能·python·算法·决策树·机器学习·数据挖掘·剪枝
喵手8 分钟前
Python爬虫实战:城市停车收费标准自动化采集系统 - 让停车费透明化的技术实践(附CSV导出 + SQLite持久化存储)!
爬虫·python·爬虫实战·零基础python爬虫教学·城市停车收费标准·采集城市停车收费数据·采集停车数据csv文件导出
无水先生9 分钟前
python函数的参数管理(01)*args和**kwargs
开发语言·python
py小王子11 分钟前
dy评论数据爬取实战:基于DrissionPage的自动化采集方案
大数据·开发语言·python·毕业设计
Pyeako13 分钟前
opencv计算机视觉--LBPH&EigenFace&FisherFace人脸识别
人工智能·python·opencv·计算机视觉·lbph·eigenface·fisherface
小陶的学习笔记17 分钟前
python~基础
开发语言·python·学习
多恩Stone17 分钟前
【3D AICG 系列-9】Trellis2 推理流程图超详细介绍
人工智能·python·算法·3d·aigc·流程图
ID_1800790547319 分钟前
Python结合淘宝关键词API进行商品价格监控与预警
服务器·数据库·python
玄同76523 分钟前
Python 自动发送邮件实战:用 QQ/163 邮箱发送大模型生成的内容
开发语言·人工智能·python·深度学习·机器学习·邮件·邮箱
岱宗夫up24 分钟前
神经网络(MLP)在时间序列预测中的实践应用
python