Python学习笔记-校验源与备份目录差异

简介:

校验源与备份目录差异

    有时我们无法确认备份与源目录文件是否保持一致,包括源目录中的新文件或目录、更新文件或目录有无成功同步,定期进行校验,没有成功则希望有针对性地进行补备份。

本例使用了filecmp模块的left_only、diff_files方法递归获取源目录的更新项,再通过shutil.copyfile、os.makedirs方法对更新项进行复制,最终保持一致状态。

 

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
#!/usr/bin/python3
#
import  os
import  sys
import  filecmp
import  re
import  shutil
holderlist = []
 
def  compareme(dir1,dir2):    #递归获取更新项函数
     dircomp  =  filecmp.dircmp(dir1,dir2)
     only_in_one  =  dircomp.left_only      #源目录新文件或目录
     diff_in_one  =  dircomp.diff_files     #不匹配文件,源目录文件已经发生变化
     dirpath  =  os.path.abspath(dir1)      #定义源目录绝对路径
     [holderlist.append(os.path.abspath(os.path.join(dir1,x)))  for  in  only_in_one]
     [holderlist.append(os.path.abspath(os.path.join(dir1,x)))  for  in  diff_in_one]
 
     if  len (dircomp.common_dirs) >  0 :     #判断是否存在相同子目录,以便递归
         for  item  in  dircomp.common_dirs:     #递归子目录
             compareme(os.path.abspath(os.path.join(dir1,item)), os.path.abspath(os.path.join(dir2,item)))
     return  holderlist
def  checkargv():
     if  len (sys.argv) <  2 :    #要求输入源目录与备份目录
         print  ( "Usage: " , sys.argv[ 0 ],  "datadir backupdir" )
         sys.exit()
     else :
         dir1  =  sys.argv[ 1 ]
         dir2  =  sys.argv[ 2 ]
         source_files  =  compareme(dir1,dir2)      #对比源目录与备份目录
         dir1  =  os.path.abspath(dir1)
 
         if  not  dir2.endswith( '/' ):       #备份目录路径加“/”符
             dir2  =  dir2 + '/'     
         dir2  =  os.path.abspath(dir2)
         destination_files  =  []
         createdir_bool  =  False
 
         for  item  in  source_files:    #遍历返回的差异文件或目录清单
             destination_dir  =  re.sub(dir1,dir2,item)     #将源目录差异路径清单对应替换成备份目录
             destination_files.append(destination_dir)  
             if  os.path.isdir(item):      #如果差异路径为目录且不存在,则再备份目录中创建
                 if  not  os.path.exists(destination_dir):
                     os.makedirs(destination_dir)
                     createdir_bool  =  True    #再次调用compareme函数标记
 
         if  createdir_bool:       #重新调用compareme函数,重新遍历新创建目录的内容
             destination_files  =  []
             source_files  =  []
             source_files  =  compareme(dir1,dir2)      #调用compareme函数
             for  item  in  source_files:    #获取源目录差异路径清单,对应替换成备份目录
                 destination_dir  =  re.sub(dir1,dir2,item)
                 destination_files.append(destination_dir)
 
         print  ( "update item: " )
         print  (source_files)     #输出更新项列表清单
         copy_pair  =  zip (source_files,destination_files)      #讲源目录与备份目录文件清单拆分成元组
         for  item  in  copy_pair:
             if  os.path.isfile(item[ 0 ]):      #判断是否为文件,是则进行复制操作
                 shutil.copyfile(item[ 0 ], item[ 1 ])
 
if  __name__  = =  '__main__' :
     checkargv()


执行前:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
[root@kurol pkg1] # tree -d test1 test2
test1
| - -  test11
| - -  test12
| - -  test13
| - -  test17
| - -  test99
` - -  testmmmm13
test2
| - -  test11
| - -  test12
| - -  test13
| - -  test14
` - -  test17
 
11  directories


执行:

1
2
3
[root@kurol pkg1] # python3 checkdir2.py test1 test2
update item: 
[ '/opt/python361/pkg1/test1/test99' '/opt/python361/pkg1/test1/testmmmm13' ]


执行后:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
[root@kurol pkg1] # tree -d test1 test2
test1
| - -  test11
| - -  test12
| - -  test13
| - -  test17
| - -  test99
` - -  testmmmm13
test2
| - -  test11
| - -  test12
| - -  test13
| - -  test14
| - -  test17
| - -  test99
` - -  testmmmm13
 
13  directories






      本文转自谢育政 51CTO博客,原文链接:http://blog.51cto.com/kurolz/1935047 ,如需转载请自行联系原作者


相关文章
|
2天前
|
存储 Python 容器
Python编程基础第二天学习笔记
Python编程的第二天学习是建立在基础概念上的深化和扩展,强调了基本语法、数据类型、控制结构和函数的重要性。通过实践这些概念,可以增强对Python编程语言的理解,并为后续的高级学习打下坚实的基础。继续实践并逐渐探索更复杂的编程任务将有助于巩固和扩展这些基础知识。
19 7
|
20天前
|
安全 项目管理 Python
使用Python shutil库进行文件和目录操作
使用Python shutil库进行文件和目录操作
使用Python shutil库进行文件和目录操作
|
1月前
|
存储 索引 Python
Python学习笔记----列表、元组和字典的基础操作
这篇文章是一份Python学习笔记,涵盖了列表、元组和字典的基础操作,包括它们的创建、修改、删除、内置函数和方法等。
Python学习笔记----列表、元组和字典的基础操作
|
1月前
|
Python
Python学习笔记---函数
这篇文章是一份Python函数学习的笔记,涵盖了使用函数的优势、内置函数的调用、自定义函数的定义、函数参数的不同类型(必须参数、关键字参数、默认参数、可变参数)、有返回值和无返回值的函数、形参和实参、变量作用域、返回函数、递归函数、匿名函数、偏函数以及输入和输出函数等多个函数相关的主题。
|
1月前
|
索引 Python
Python学习笔记----操作字符串
这篇文章是一份Python字符串操作的学习笔记,涵盖了字符串相加、序列相加、字符串长度和字符的查找、统计、分割、连接、替换、去除空白、大小写转换以及判断字符串是否由字母和数字组成等常用方法。
Python学习笔记----操作字符串
|
1月前
|
Python
python学习笔记---流程控制
这篇文章详细介绍了Python中的流程控制,包括选择结构(if、if-else语句、嵌套if语句)和循环语句(while循环、for循环以及for循环与range()函数的使用),以及如何在循环中使用break和continue语句。
python学习笔记---流程控制
|
1月前
|
索引 Python
python学习笔记----必备知识
这篇文章是一份全面的Python学习笔记,涵盖了Python的必备知识,包括语法特点、流程控制、数据类型、运算符、输入输出方法,以及对序列、字符串、正则表达式、函数、面向对象程序设计、模块和包的介绍。
python学习笔记----必备知识
|
1月前
|
Python
[python]为指定目录下的文件名批量加前缀
[python]为指定目录下的文件名批量加前缀
|
3月前
|
存储 开发者 C++
Python教程:Python安装目录说明
在 Python 开发中,深入了解 Python 的安装目录结构对于开发者来说是至关重要的。本文以Python 3.8.6为例,详细介绍 Python 的安装目录结构、各个子目录和文件的作用。
74 4
|
2月前
|
存储 Python
`tempfile`模块在Python中用于创建临时文件和目录。
`tempfile`模块在Python中用于创建临时文件和目录。