当前位置: 首页 > news >正文

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

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

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回收站中的文件和目录

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回收站中的文件和目录

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()

 

相关文章:

  • 北京网站建设多少钱?
  • 辽宁网页制作哪家好_网站建设
  • 高端品牌网站建设_汉中网站制作
  • MinIO实战攻略:轻松构建私有云存储解决方案
  • streamlit+wordcloud使用pyinstaller打包遇到的一些坑
  • boost库容器之Circular Buffer功能介绍,及使用示例
  • 神经网络微调技术全解(04)-- Prompt Tuning-可训练提示(Learnable Prompts)
  • 第十章 rust网络编程基础
  • 基于web的停车场管理系统设计与实现-计算机毕设 附源码 16856
  • Kafka·概述
  • 【计算机网络】计算机网络的分层结构
  • 【深度学习与NLP】——深度卷积神经网络AlexNet
  • 华为eNSP:静态路由配置、浮动路由配置
  • 阿里云OSS文件存储
  • 初识C++(8.27)
  • CentOS 7的yum源更换为国内源
  • 【设计模式】简单工厂模式
  • Ai+若依(页面调整--去除若依的各种痕迹,采用自己的):【07篇】
  • 时间复杂度分析经典问题——最大子序列和
  • 【JavaScript】通过闭包创建具有私有属性的实例对象
  • 4个实用的微服务测试策略
  • 8年软件测试工程师感悟——写给还在迷茫中的朋友
  • AzureCon上微软宣布了哪些容器相关的重磅消息
  • iOS编译提示和导航提示
  • java架构面试锦集:开源框架+并发+数据结构+大企必备面试题
  • Laravel 实践之路: 数据库迁移与数据填充
  • Linux学习笔记6-使用fdisk进行磁盘管理
  • Lsb图片隐写
  • Mithril.js 入门介绍
  • Python 使用 Tornado 框架实现 WebHook 自动部署 Git 项目
  • python3 使用 asyncio 代替线程
  • Sequelize 中文文档 v4 - Getting started - 入门
  • vue2.0项目引入element-ui
  • Xmanager 远程桌面 CentOS 7
  • 测试开发系类之接口自动化测试
  • 程序员最讨厌的9句话,你可有补充?
  • 二维平面内的碰撞检测【一】
  • 入职第二天:使用koa搭建node server是种怎样的体验
  • 突破自己的技术思维
  • 网络应用优化——时延与带宽
  • 我建了一个叫Hello World的项目
  • 学习ES6 变量的解构赋值
  • gunicorn工作原理
  • ​LeetCode解法汇总518. 零钱兑换 II
  • #[Composer学习笔记]Part1:安装composer并通过composer创建一个项目
  • #gStore-weekly | gStore最新版本1.0之三角形计数函数的使用
  • #我与Java虚拟机的故事#连载17:我的Java技术水平有了一个本质的提升
  • (1)(1.19) TeraRanger One/EVO测距仪
  • (1)bark-ml
  • (C语言)共用体union的用法举例
  • (Forward) Music Player: From UI Proposal to Code
  • (ISPRS,2021)具有遥感知识图谱的鲁棒深度对齐网络用于零样本和广义零样本遥感图像场景分类
  • (SERIES12)DM性能优化
  • (第9篇)大数据的的超级应用——数据挖掘-推荐系统
  • (欧拉)openEuler系统添加网卡文件配置流程、(欧拉)openEuler系统手动配置ipv6地址流程、(欧拉)openEuler系统网络管理说明
  • (十二)springboot实战——SSE服务推送事件案例实现
  • (转) Face-Resources
  • (转)C语言家族扩展收藏 (转)C语言家族扩展