搜索
bottom↓
回复: 6

Python实现备份文件程序

[复制链接]

出0入0汤圆

发表于 2018-8-1 16:22:43 | 显示全部楼层 |阅读模式
本帖最后由 Gohome_soon 于 2018-8-1 16:22 编辑

学习Python有一段时间了,了解了基本语法,自己尝试写了一个文件备份程序。
这个程序还有许多bug,能力问题,看以后要在优化吧
参考了https://www.cnblogs.com/xiao1/p/6171064.html

update_form(需要备份的源文件)                默认设置成 'E:\\WorkStation\\12-Ubuntu\\scwLinux\\wSation\\'
update_to        (备份文件存储位置)                默认是 'G:\\WorkStation\\12-Ubuntu\\scwLinux\\wSation\\'
这些需要在在实例化时设置:update_t = update(update_form = "...",update_to="...")

原文中使用              import re  
                        destination_dir = re.sub(dir1,dir2,item)
来替换需要备份的文件夹路径,而我在调用中出现错误,没有能力解决,所以我固定使用 destination_dir = item.replace("E:\\","G:\\") ,这点需要更改的,没有实现自动匹配


  1. #coding:utf-8
  2. import os
  3. import filecmp
  4. import shutil
  5. import time
  6. class update(object):
  7.         def __init__ (self,update_from = None,update_to = None):
  8.                 if update_from is None:
  9.                         self.__update_from = 'E:\\WorkStation\\12-Ubuntu\\scwLinux\\wSation\\'#备份target_dir文件到back_dir
  10.                 else:
  11.                         self.__update_from = update_from

  12.                 if update_to is None:
  13.                         self.__update_to = 'G:\\WorkStation\\12-Ubuntu\\scwLinux\\wSation\\'#将target_dir文件备份到back_dir
  14.                 else:
  15.                         self.__update_to = update_to

  16.                 self.holderlist = []

  17.         def __compareme(self,dir1,dir2):    #递归获取更新项函数
  18.                 dircomp = filecmp.dircmp(dir1,dir2)
  19.                 only_in_one = dircomp.left_only      #只在左边文件夹中存在的文件或文件夹;
  20.                 diff_in_one = dircomp.diff_files     #不匹配文件,源目录文件已发生变化
  21.                 dirpath = os.path.abspath(dir1)      #定义源目录绝对路径

  22.                 #只在左边存在的文件中如果是文件夹,则创建
  23.                 for item in only_in_one :
  24.                         #判断只有左边有的文件是文件夹或者其他
  25.                         item_file=os.path.abspath(os.path.join(dir1,item))
  26.                         if os.path.isdir(item_file): #如果是文件夹
  27.                                 item_t=os.path.abspath(os.path.join(dir2,item))                                   
  28.                                 if not os.path.exists(item_t):#在右边创建改文件夹
  29.                                         os.makedirs(item_t)
  30.                                         dircomp.common_dirs.append(item)#加入共同拥有的列表中,便于下面递归调用
  31.                         else:
  32.                                 self.holderlist.append(item_file)#如果是文件,则将改文件路径添加入holderlist中
  33.                 #将更新文件或目录追加到holderlist
  34.                 [ self.holderlist.append(os.path.abspath(os.path.join(dir1,x))) for x in diff_in_one ]
  35.                 if len(dircomp.common_dirs) > 0:  #判断是否存在相同子目录,以便递归
  36.                         for item in dircomp.common_dirs:   #递归子目录
  37.                                 self.__compareme(os.path.abspath(os.path.join(dir1,item)),os.path.abspath(os.path.join(dir2,item)))
  38.                 return self.holderlist

  39.         def messagStr (self,dirstr):
  40.                 outDirStr = ''
  41.                 if len(dirstr) < 30:
  42.                         outDirStr = dirstr
  43.                 else:
  44.                         outDirStr = '...'
  45.                         endNum = len(dirstr)
  46.                         startNum = endNum-30

  47.                         while True:
  48.                                 if not dirstr[startNum] == '\\':
  49.                                         if startNum < endNum:
  50.                                                 startNum = startNum+1
  51.                                         else:
  52.                                                 startNum = 30
  53.                                                 break
  54.                                 else:
  55.                                         break

  56.                         outDirStr += dirstr[startNum:endNum]
  57.                 return outDirStr

  58.                  
  59.         def update(self):
  60.                 destination_files=[]
  61.                 source_files = self.__compareme(self.__update_from,self.__update_to)    #对比源目录与备份目录
  62.                 self.__update_from = os.path.abspath(self.__update_from)    #取绝对路径后,后面不会自动加上'/'
  63.                 self.__update_to = os.path.abspath(self.__update_to)
  64.                 for item in source_files:     #遍历返回的差异文件或目录清单
  65.                         destination_dir = item.replace("E:\","G:\")
  66.                         destination_files.append(destination_dir)
  67.                
  68.                 copy_pair = zip(source_files,destination_files)  #将源目录与备份目录文件清单拆分成元组
  69.                 num=0
  70.                 if len(source_files)>0:
  71.                         print('update :')
  72.                         for item in copy_pair:
  73.                                 if os.path.isfile(item[0]):        #判断是否为文件,是则进行复制操作
  74.                                         outStr = self.messagStr(item[0])
  75.                                         print('\t%s update:\t%s'%(time.strftime("%H-%M-%S"),outStr))     #输出更新项列表清单
  76.                                         num += 1
  77.                                         shutil.copyfile(item[0],item[1])
  78.                 else:
  79.                         print('nothing to update !')
  80. if __name__ == "__main__":
  81.         update_t = update()
  82.         update_t.update()
复制代码



本帖子中包含更多资源

您需要 登录 才可以下载或查看,没有帐号?注册

x

出0入0汤圆

发表于 2018-8-1 18:37:06 | 显示全部楼层
楼主有什么比较好的入门的学习资料吗 我也想学学

出0入0汤圆

发表于 2018-8-1 19:17:22 | 显示全部楼层
destination_dir = re.sub(r"[a-zA-Z]:", "G:", item)

出0入0汤圆

 楼主| 发表于 2018-8-1 20:23:27 | 显示全部楼层
apeng2012 发表于 2018-8-1 19:17
destination_dir = re.sub(r"[a-zA-Z]:", "G:", item)

能详细说明下吗,有点看不懂

出0入0汤圆

 楼主| 发表于 2018-8-1 20:24:17 | 显示全部楼层
不再犹豫 发表于 2018-8-1 18:37
楼主有什么比较好的入门的学习资料吗 我也想学学

网上找的资料,也没专门看什么说,了解了一些基本语法,不深入

出50入255汤圆

发表于 2018-8-1 21:14:18 | 显示全部楼层
www.liaoxufeng.com

出0入0汤圆

发表于 2018-8-1 22:49:27 | 显示全部楼层
这种好像linux一条 sync命令就搞定了
win下 就不知道了
回帖提示: 反政府言论将被立即封锁ID 在按“提交”前,请自问一下:我这样表达会给举报吗,会给自己惹麻烦吗? 另外:尽量不要使用Mark、顶等没有意义的回复。不得大量使用大字体和彩色字。【本论坛不允许直接上传手机拍摄图片,浪费大家下载带宽和论坛服务器空间,请压缩后(图片小于1兆)才上传。压缩方法可以在微信里面发给自己(不要勾选“原图),然后下载,就能得到压缩后的图片】。另外,手机版只能上传图片,要上传附件需要切换到电脑版(不需要使用电脑,手机上切换到电脑版就行,页面底部)。
您需要登录后才可以回帖 登录 | 注册

本版积分规则

手机版|Archiver|amobbs.com 阿莫电子技术论坛 ( 粤ICP备2022115958号, 版权所有:东莞阿莫电子贸易商行 创办于2004年 (公安交互式论坛备案:44190002001997 ) )

GMT+8, 2024-4-18 15:24

© Since 2004 www.amobbs.com, 原www.ourdev.cn, 原www.ouravr.com

快速回复 返回顶部 返回列表