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

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设计】。

相关文章

Python基础之函数用法实例详解

本文以实例形式较为详细的讲述了Python函数的用法,对于初学Python的朋友有不错的借鉴价值。分享给大家供大家参考之用。具体分析如下: 通常来说,Python的函数是由一个新的语句编...

Python matplotlib学习笔记之坐标轴范围

Python matplotlib学习笔记之坐标轴范围

Python学习笔记--坐标轴范围 参靠视频:《Python数据可视化分析 matplotlib教程》链接:https://www.bilibili.com/video/av698941...

Python读取stdin方法实例

Python读取stdin方法实例

Python中常用到的两种标准化输入方式:分别sys.stdin和input,两者使用方式大致相同,但是总的来说sys.stdin使用方式更加多样化一些,下面就例子说明两者之间的使用差别...

Python的类实例属性访问规则探讨

一般来说,在Python中,类实例属性的访问规则算是比较直观的。 但是,仍然存在一些不是很直观的地方,特别是对C++和Java程序员来说,更是如此。 在这里,我们需要明白以下几个地方:...

简单了解python中的与或非运算

简单了解python中的与或非运算

真的很重要,栽了个跟头!!!(虽然以前好像知道。。。) print(True or False and False) print((True or False) and False)...