使用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()
相关推荐
计算机毕业编程指导师2 分钟前
【计算机毕设推荐】Python+Spark卵巢癌风险数据可视化系统完整实现 毕业设计 选题推荐 毕设选题 数据分析 机器学习 数据挖掘
hadoop·python·计算机·数据挖掘·spark·毕业设计·卵巢癌
玩转单片机与嵌入式4 分钟前
学习嵌入式AI(TInyML),只需掌握这点python基础即可!
人工智能·python·学习
少年执笔7 分钟前
ollama搭建本地模型框架
python·ai
极光代码工作室10 分钟前
基于大数据的校园消费行为分析系统
大数据·hadoop·python·数据分析·spark
A__tao1 小时前
JSON 转 Java 实体类工具(支持嵌套与注释解析)
java·python·json
zhouwy1131 小时前
Python 基础语法笔记:从入门到进阶的系统学习
python
高洁011 小时前
工程科研中的AI应用:结构力学分析技巧
python·深度学习·机器学习·数据挖掘·知识图谱
大邳草民2 小时前
Python 爬虫:从 HTTP 请求到接口分析
笔记·爬虫·python
众创岛2 小时前
Playwright 元素定位
python·自动化
艾派森2 小时前
深度学习实战-基于EfficientNetB5的家禽鸡病图像分类识别模型
人工智能·python·深度学习·神经网络·分类