“Python云存储备份”的版本间的差异

来自CloudWiki
跳转至: 导航搜索
增量备份
第34行: 第34行:
 
===广度优先遍历目录===
 
===广度优先遍历目录===
  
 +
<nowiki>
 +
from os.path import join, isfile, isdir
  
<nowiki>import os
+
def listDirWidthFirst(directory):
from collections import deque#从收集模块中导入双端队列
+
    '''广度优先遍历文件夹'''
class GuangDu:
+
    #使用列表模拟双端队列,效率稍微受影响,不过关系不大
    def __init__(self,path):
+
    dirs = [directory]#这个directory就是列表的第一个元素
        "初始换函数,读取的根目录"
+
    #如果还有没遍历过的文件夹,继续循环
        self.path =path
+
    while dirs:
        self.MyList =deque([])#实例化一个队列
+
        #遍历还没遍历过的第一项
         self.MyList.append(self.path)#把根目录路径放入队列中
+
         current = dirs.pop(0)
          
+
         #遍历该文件夹,如果是文件就直接输出显示
    def  BianLi(self):
+
         #如果是文件夹,输出显示后,标记为待遍历项
         "广度遍历的方法实现"
+
         for subPath in listdir(current):
         while len(self.MyList) !=0:#当队列中为空的时候跳出循环
+
             path = join(current, subPath)
             path =self.MyList.popleft()#从队列中弹出一个路径
+
             if isfile(path):
             if os.path.isdir(path):#对弹出的path路径判断是否是一个文件夹
+
                 print(path)
                 print("文件夹",path)#打印文件夹的路径
+
            elif isdir(path):
                myFilePath =os.listdir(path)#如果是一个文件夹,就把文件夹里面的所有东西添加进列表中,
+
                 print(path)
                 for line in myFilePath:#对添加到列表中的东西进行遍历
+
                dirs.append(path)
                    myPath =path +"\\"+line#形成绝对路径,
 
                    self.MyList.append(myPath)#把遍历的东西都加入到队列中
 
            else:#如果不是一个文件夹,就直接把路径打印出来,不用对其进行遍历了
 
                print("文件",path)
 
 
                  
 
                  
    def __del__(self):
+
listDirWidthFirst(r'D:\Game')</nowiki>
        "最终会执行的函数"
+
 
        pass
+
===深度优先遍历目录===
 +
===复制云端目录(选做)===
 +
<nowiki>
 +
from os import listdir,makedirs
 +
from os.path import join, isfile, isdir
 +
 
 +
dirs= []
 +
dirs_right =[]  #云端的目录集合
 +
dirs_left = []  #本地端的目录集合
 +
 
 +
def GetDirWidthFirst(root_right):#root_right:云端根目录
 +
    '''广度优先遍历,得到云端目录集合'''
 +
    #使用列表模拟双端队列,效率稍微受影响,不过关系不大
 +
    global dirs,dirs_right
 +
    dirs = [root_right] #dirs:用做广度优先遍历的列表,这个root_right就是列表的第一个元素
 +
    dirs_right = [root_right]
 +
    #如果还有没遍历过的文件夹,继续循环
 
      
 
      
path =r"F:\广度遍历测试"#初始的文件目录
+
    while dirs:
file =GuangDu(path)#实例化一个对象
+
        #遍历还没遍历过的第一项
file.BianLi()#对象调用方法
+
        current = dirs.pop(0)
 +
        #遍历该文件夹,如果是文件就直接输出显示
 +
        #如果是文件夹,输出显示后,标记为待遍历项
 +
        for subPath in listdir(current):
 +
            #print(subPath)           
 +
            path = join(current,subPath)
 +
            #print(path)
 +
            #os.path.split(path)
 +
            #('D:\\teaching\\自动化运维\\练习', '7.1.py')
 +
            #current.split("teaching\\")
 +
            a,b=current.split("teaching\\")
 +
            #print(a,b)           
 +
            if isdir(path):
 +
                #print(path)
 +
                dirs.append(path)
 +
                dirs_right.append(path)
 +
 
 +
def CreateDirLocal(root_left,root_right):
 +
    '''创建本地目录'''
 +
    global dirs,dirs_right,dirs_left
  
 +
    #生成本地目录名
 +
    for d in dirs_right:   
 +
        #print(d)
 +
        new_dir =d.replace(root_right,root_left)
 +
        dirs_left.append(new_dir)
 +
       
 +
    total,new=0,0   
 +
    for d in dirs_left:   
 +
        #print(d)
 +
        total+=1
 +
        if not isdir(d):
 +
            print(d,"不存在")
 +
            makedirs(d)
 +
            new+=1
 +
    print("total=",total,"new=",new)
 +
   
 +
root_right=r'E:\20200311\teaching\自动化运维'     
 +
root_left=r'D:\teaching\自动化运维'         
 +
GetDirWidthFirst(root_right)
 +
CreateDirLocal(root_left,root_right)
 
</nowiki>
 
</nowiki>
  
第108行: 第161行:
 
      
 
      
 
      
 
      
right=r'E:\下载'       
+
root_right=r'E:\20200311'       
left='D:\\我爱学习\网络安全'
+
root_left='D:\\'
  
 +
d=r"teaching\自动化运维"
 +
left=join(root_left,d)
 +
right=join(root_right,d)
 +
print(left,right)
 
CopyToCloud(left,right)</nowiki>
 
CopyToCloud(left,right)</nowiki>

2020年6月12日 (五) 03:09的版本

基础操作

比较左右两侧文件

import filecmp

import os
left = r"D:\teaching\人工智能" # 定义左目录

right = r"E:\20200311\teaching\人工智能" #定义右目录

#比较目录
dirobj = filecmp.dircmp(left, right, ['cmp.py']) #目录比较,忽略test.py文件
dirobj.report()

print("left_only:")
print(dirobj.left_only) # 只在左目录中的文件或目录

复制文件

import shutil
for i in dirobj.left_only:
    left_file=os.path.realpath(i)
    right_file=os.path.join(right,i)
    print("左侧发现新文件/目录: "+i)
    print(left_file)
    print(right_file)
    s=input("是否复制?y/n")
    if s=='y':
        print("开始复制中...")
        shutil.copyfile(left_file,right_file)
        print("复制完毕"+right_file)

广度优先遍历目录

from os.path import join, isfile, isdir

def listDirWidthFirst(directory):
    '''广度优先遍历文件夹'''
    #使用列表模拟双端队列,效率稍微受影响,不过关系不大
    dirs = [directory]#这个directory就是列表的第一个元素
    #如果还有没遍历过的文件夹,继续循环
    while dirs:
        #遍历还没遍历过的第一项
        current = dirs.pop(0)
        #遍历该文件夹,如果是文件就直接输出显示
        #如果是文件夹,输出显示后,标记为待遍历项
        for subPath in listdir(current):
            path = join(current, subPath)
            if isfile(path):
                print(path)
            elif isdir(path):
                print(path)
                dirs.append(path)
                
listDirWidthFirst(r'D:\Game')

深度优先遍历目录

复制云端目录(选做)

from os import listdir,makedirs
from os.path import join, isfile, isdir

dirs= []
dirs_right =[]  #云端的目录集合
dirs_left = []  #本地端的目录集合

def GetDirWidthFirst(root_right):#root_right:云端根目录
    '''广度优先遍历,得到云端目录集合'''
    #使用列表模拟双端队列,效率稍微受影响,不过关系不大
    global dirs,dirs_right
    dirs = [root_right] #dirs:用做广度优先遍历的列表,这个root_right就是列表的第一个元素
    dirs_right = [root_right]
    #如果还有没遍历过的文件夹,继续循环
    
    while dirs:
        #遍历还没遍历过的第一项
        current = dirs.pop(0)
        #遍历该文件夹,如果是文件就直接输出显示
        #如果是文件夹,输出显示后,标记为待遍历项
        for subPath in listdir(current):
            #print(subPath)            
            path = join(current,subPath)
            #print(path)
            #os.path.split(path)
            #('D:\\teaching\\自动化运维\\练习', '7.1.py')
            #current.split("teaching\\")
            a,b=current.split("teaching\\")
            #print(a,b)            
            if isdir(path):
                #print(path)
                dirs.append(path)
                dirs_right.append(path)

def CreateDirLocal(root_left,root_right):
    '''创建本地目录'''
    global dirs,dirs_right,dirs_left

    #生成本地目录名
    for d in dirs_right:    
        #print(d)
        new_dir =d.replace(root_right,root_left)
        dirs_left.append(new_dir)
        
    total,new=0,0    
    for d in dirs_left:    
        #print(d)
        total+=1
        if not isdir(d):
            print(d,"不存在")
            makedirs(d)
            new+=1
    print("total=",total,"new=",new)
    
root_right=r'E:\20200311\teaching\自动化运维'      
root_left=r'D:\teaching\自动化运维'          
GetDirWidthFirst(root_right)
CreateDirLocal(root_left,root_right)

应用实战

增量备份

本程序用于将文件增量备份到云端:

from os import listdir,makedirs
from os.path import join, isfile, isdir
import os
import shutil,filecmp

def usage():
    print("目录路径不对 ~")  




def CopyToCloud(scrDir, dstDir):
    if ((not os.path.isdir(scrDir)) or (not os.path.isdir(dstDir)) or
      (os.path.abspath(scrDir)!=scrDir) or (os.path.abspath(dstDir)!=dstDir)):
        usage()
        return
        
    for item in os.listdir(scrDir):
        scrItem = os.path.join(scrDir, item)
        dstItem = scrItem.replace(scrDir,dstDir)
        if os.path.isdir(scrItem):
            #创建新增的文件夹,保证目标文件夹的结构与原始文件夹一致
            if not os.path.exists(dstItem):
                os.makedirs(dstItem)
                print('make directory'+dstItem)
            #递归调用自身函数
            CopyToCloud(scrItem, dstItem)
        elif os.path.isfile(scrItem):
            #只复制新增或修改过的文件
            if ((not os.path.exists(dstItem)) or
               (not filecmp.cmp(scrItem, dstItem, shallow=False))):
                shutil.copyfile(scrItem, dstItem)
                print('file:'+scrItem+'==>'+dstItem)
    
    
    
    
root_right=r'E:\20200311'      
root_left='D:\\'

d=r"teaching\自动化运维"
left=join(root_left,d)
right=join(root_right,d)
print(left,right)
CopyToCloud(left,right)