恢复百度云盘本地误删的文件脚本(简单方法)

yipeiwu_com5年前Python基础

今天被同步盘搞得焦头烂额。

辛苦码的代码(除了重要的、备份过的)都被删掉了……

当时我就石化了。。。

随后发现同步盘目录有个delete目录,里面还有manifest.xml,和一堆改了名的文件,

看到manifest.xml的内容时,瞬间觉得有救了,立马开搞python

废话不多说,直接上代码:

#-*- coding:utf-8 -*-
from xml.etree import ElementTree
import os
import sys
reload(sys)
sys.setdefaultencoding( "utf-8" )

def convertfile(cachePath,orgPath):
 '''恢复文件'''
 start=0;
 while True:
  index = orgPath.find('\\', start)
  if index == -1:
   break
  start = index + 1

 orgDir=orgPath[:start]
 print 'orgDir:',orgDir

 if not os.path.exists(orgDir): 
  os.makedirs(orgDir) 
 if not os.path.exists(orgPath) or(os.path.exists(orgPath) and (os.path.getsize(orgPath) != os.path.getsize(cachePath))): 
  file_in=open(cachePath, "rb")
  file_out=open(orgPath, "wb")
  file_out.write(file_in.read()) 
  file_in.close()
  file_out.close()
 
 
def read_xml(text):
 '''读xml文件'''
 root = ElementTree.fromstring(text)
 
 lst_node = root.getiterator("record")
 for node in lst_node:
  cp=node.attrib['cachePath']
  op=node.attrib['orgPath']
  cp=cp.replace('~','.')
  op=op.replace('~','.')
  print cp+'->'+op
  convertfile(cp,op)
 
if __name__ == '__main__':
 '''将本文件放在云同步盘的根目录下,
  将mani_file改为需要恢复的manifest文件'''
 mani_file=".\\.baohe.cache\\.delete\\20140412\\manifest.xml"
 read_xml(open(mani_file).read())

本文件在Python2.7.6下正常,3.4貌似有问题(汗)

python可以在官网下载:https://www.python.org/downloads/

将本文件(假如叫做huifu.py)放在云同步盘的根目录下,比如云同步盘在“d:\baiduyun\”,那么文件应该在“d:\baiduyun\”下,最终是这样的“d:\baiduyun\huifu.py

千万不要轻易从百度云上删除已经上传的文件啊!血泪教训。。。

以上这篇恢复百度云盘本地误删的文件脚本(简单方法)就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持【听图阁-专注于Python设计】。

相关文章

opencv python 基于KNN的手写体识别的实例

OCR of Hand-written Data using kNN OCR of Hand-written Digits 我们的目标是构建一个可以读取手写数字的应用程序, 为此,我...

python多线程高级锁condition简单用法示例

本文实例讲述了python多线程高级锁condition简单用法。分享给大家供大家参考,具体如下: 多线程编程中如果使用Condition对象代替lock, 能够实现在某个事件触发后才处...

Django Form and ModelForm的区别与使用

Form介绍 在HTML页面中利用form表单向后端提交数据时,都会写一些获取用户输入的标签并且用form标签把它们包起来。 与此同时我们在好多场景下都需要对用户的输入做校验,比如校验...

Python实例方法、类方法、静态方法的区别与作用详解

本文实例讲述了Python实例方法、类方法、静态方法的区别与作用。分享给大家供大家参考,具体如下: Python中至少有三种比较常见的方法类型,即实例方法,类方法、静态方法。它们是如何定...

Python的Django框架中模板碎片缓存简介

你同样可以使用cache标签来缓存模板片段。 在模板的顶端附近加入{% load cache %}以通知模板存取缓存标签。 模板标签{% cache %}在给定的时间内缓存了块的内容。...