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

embedded/2024/9/24 0:20:48/

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

python">import os
import platformdef put_back_trash():# 获取操作系统类型os_type = platform.system()if os_type == "Windows":  # Windowsfrom winshell import recycle_binfor 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.parsedef 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 Nonedef 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}')returnif os.path.exists(original_path):print(f'Error: File {original_path} already exists')returntrash_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":  # Linuxrestore_linux_all_files()else:print("Unsupported operating system.")put_back_trash()

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

python">import os
import platform
import shutil
import urllib.parsedef put_back_trash():# 获取操作系统类型os_type = platform.system()if os_type == "Darwin":  # macOSdef generate_applescript():script = '''tell application "Finder"activateset file_count to count of (trash's items)-- log file_countrepeat file_count timesrecoverMyFile() of meend repeatend tellon recoverMyFile()tell application "System Events"set frontmost of process "Finder" to truetell application "Finder"open trashselect the first item of front windowend telltell process "Finder"key code 51 using command downdelay 2 -- Yes, it's stupid, but necessary :(end tellend tellend 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()

 


http://www.ppmy.cn/embedded/101829.html

相关文章

QGC地面站连接roslanch创建的仿真无人机

今天遇到這個情況,看了網上的方法依然沒有解決 QGC地面站无法连接roslanch创建的仿真无人机-问答专区-Amovlab阿木实验室-让机器人研发更高效! - 問題的根源應該是沒有進入sitl的環境。這裏寫一下解決方法: 首先編譯 export Autopilot_DIR…

QT-五子棋游戏

QT-五子棋游戏 一、演示效果二、核心代码三、下载链接 一、演示效果 二、核心代码 #include "GameModel.h" #include <time.h> #include <stdlib.h>GameModel::GameModel(){}void GameModel::startGame(GameType type){gameType type;//初始化棋盤game…

小程序组件生命周期和获取组件实例

组件实例 1 selectComponent 方法概述 selectComponent 是微信小程序提供的一个 API&#xff0c;用于在页面或组件内部获取子组件的实例。通过获取组件实例&#xff0c;可以&#xff1a; 调用组件内部的方法&#xff1b;访问组件的内部数据&#xff1b;进行组件间的通信&…

2024河南省萌新联赛第(六)场 郑州大学

文章目录 2024河南省萌新联赛第&#xff08;六&#xff09;场 郑州大学A.装备二选一&#xff08;一&#xff09;题意&#xff1a;思路&#xff1a;AC代码&#xff1a; B.百变吗喽题意&#xff1a;思路&#xff1a;AC代码&#xff1a; C.16进制世界题意&#xff1a;思路&#xf…

Qt (11)【Qt窗口 —— 对话框 | Qt内置对话框简介】

阅读导航 引言一、对话框1. 对话框的分类&#xff08;1&#xff09;模态对话框&#xff08;2&#xff09;非模态对话框 二、Qt内置对话框 引言 在上一篇文章中&#xff0c;我们深入探讨了Qt框架中窗口的基本构建块&#xff0c;它们共同构成了Qt应用程序中用户界面&#xff08;…

wpf livechart 绘制笛卡尔曲线

先上图&#xff1a; 代码部分&#xff1a; <GroupBox Header"各生产线生产量趋势"><Grid><Grid.RowDefinitions><RowDefinition Height"45"/><RowDefinition Height"auto"/><RowDefinition/></Grid.RowD…

Spring中Bean的生命周期管理

Spring框架中的Bean生命周期是指从创建到销毁的整个过程。在这个过程中&#xff0c;Spring容器会负责管理Bean的状态和行为。以下是Spring中Bean生命周期的详细解释&#xff1a; 实例化&#xff1a;Spring容器根据Bean的定义信息&#xff0c;通过反射机制创建Bean对象。 设置属…

【国产游戏的机遇与挑战】

&#x1f49d;&#x1f49d;&#x1f49d;欢迎来到我的博客&#xff0c;很高兴能够在这里和您见面&#xff01;希望您在这里可以感受到一份轻松愉快的氛围&#xff0c;不仅可以获得有趣的内容和知识&#xff0c;也可以畅所欲言、分享您的想法和见解。 推荐:kwan 的首页,持续学…