当前位置: 首页 > 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()

 


http://www.mrgr.cn/news/8412.html

相关文章:

  • Windows与Linux的和谐共处时代!
  • 目标 CDC实例数据库更改密码,预定启动报错SQL 错误代码为“-30082”。SQL 状态为:08001。
  • docker配置国内镜像加速
  • Vue项目中根据电脑的分辨率使用了Zoom缩放导致日期控件和下拉框控件位置偏移;
  • 【C语言】内存函数
  • 如何使用ssm实现基于HTML5的出租车管理系统
  • NLP位置编码
  • 基于Springboot2 + vue3酒店客房预订管理系统
  • rust 日志记录与跟踪
  • 游戏开发设计模式之责任链模式
  • Gameplay Ability System(通过表配置不同等级的伤害)
  • Ruby跨平台移动应用开发的新篇章
  • 国货之光|暴雨机推出面向大模型训练的AI服务器
  • 【Node】【2】创建node应用
  • 汇编语句中的 jmp 与 call 指令
  • 微服务事务管理
  • GATK AlleleList接口介绍
  • 鸿蒙学习(四):泛型空安全模块导入导出
  • 分享从零开始学习网络设备配置--任务6.2 实现网络设备的远程管理
  • C语言 | Leetcode C语言题解之第355题设计推特