Python Notes series: File contents, file and folder comparisons Difflib, Filecmp__python

Source: Internet
Author: User
Tags diff readfile
File content comparison #!/usr/bin/python import difflib Text1 = "" Text1:this module provides classes and functions for comparing SEQUENC Es. including HTML and context and unified diffs. Difflib document v7.4 Add string "" "Text1_lines = Text1.splitlines () Text2 =" "Text2:this module provides classes and F Unctions for comparing sequences. including HTML and context and unified diffs. Difflib document v7.5 "" "Text2_lines = Text2.splitlines () d = difflib. Differ () diff = d.compare (text1_lines, text2_lines) print ' \ n '. Join (List (diff))
Contrast symbol meaning '-' first sequence has, second no ' + ' first sequence none, second has ' two consistent '? ' two sequences exist increment difference ' ^ ' sign out two sequence of difference characters
The Make_file () method using the Htmldiff () class generates beautiful HTML documents the following parts of the above example d = Difflib. Differ () diff = d.compare (text1_lines, text2_lines) print ' \ n '. Join (List (diff)) is replaced with d = difflib. Htmldiff () print d.make_file (text1_lines, text2_lines) file named simple2.py, execute Python simple2.py > diff.html to generate a comparison file
Nginx profile Comparison #!/usr/bin/python # coding=utf-8 import difflib import sys try:textfile1=sys.argv[1] #第一个文件路径参数 textfile2=sy S.ARGV[2] #第二个文件路径参数 except Exception,e:print "Error:" +str (e) print "Usage:simple3.py filename1 filename2" sys.exit () de F ReadFile (filename): try:filehandle = open (filename, ' RB ') Text=filehandle.read (). Splitlines () filehandle.close () ret Urn text except IOError as Error:print (' Read file error: ' +str (Error)) Sys.exit () if textfile1== "" or textfile2== "": Print "Usage:simple3.py filename1 filename2" sys.exit () Text1_lines = ReadFile (textfile1) text2_lines = ReadFile (textfile2) d = Difflib. Htmldiff () print d.make_file (Text1_lines, Text2_lines)
File execution: Python simple3.py nginx.conf.v1 nginx.conf.v2 > diff.html
Comparison of file and directory differences filecmp module three operation methods CMP single file contrast cmpfiles multiple file comparison dircmp directory comparison
FILECMP.CMP (F1,f2[,shallow]) compares file F1, F2, returns True, or false, shallow defaults to true, based only on the file base information returned by the Os.stat () method, such as last access time, modification time, When the state change time shallow to False, the Os.stat (), the contents of the file are both validated >>> import filecmp >>> filecmp.cmp ("/root/test/a", "/ Root/test/b ") False >>> filecmp.cmp ("/root/test/a ","/root/test/a ") True Filecmp.cmpfiles (dir1,dir2,common[ , shallow], which returns a list of three lists of file names, matching, mismatched, error, and error lists including files that do not exist in the directory, do not have read-write access, or other causes that cannot be compared to the list >>>filecmp.cmpfiles ("/ Root/test/dir1 ","/root/test/dir2 ", [' F1 ', ' F2 ', ' F3 ', ' F4 ', ' f5 ']") ([' F1 ', ' F2 '],[', ' F3 ',],[') ","] "F4" "," F5 "", "", "", " B[,ignore[,hide]] Ignore represents a list of ignored names, default to [' RCS ', ' CVS ', ' tags '] hide represents a hidden list, default to [Os.curdir,os.pardir] dircmp support recursion, Methods for providing three output reports: report () #比较目录中的内容 report_partial_closure () #比较目录及其第一级子目录中的内容 report_full_closure () #递归比较所有指定目录的内容 The DIRCMP class provides properties: Left directory right directory left_list files and directories in the left directory right_list files and directories in the right directory common the files and directories that are in common to the left directory left_only only files or directories T_only a subdirectory on either side of the file or directory common_dirs the right directory common_files both sides of the subdirectory Common_funny both subdirectories (different directory types or OS.STAT () error) same_files match the same file diff_files mismatched file funny_files Both sides have but cannot compare files Subdirs map Common_dirs directory names to new DIRCMP objects, in the form of dictionary types
Through DIRCMP () to achieve directory difference comparison, and output directory comparison objects all attribute information import filecmp a= "/home/test/filecmp/dir1" b= "/home/test/filecmp/dir2" dirobj= FILECMP.DIRCMP (a,b,[' test.py ']) #ignore test.py print "----------------------------------------" Dirobj.report () print "-------------report_partial_closure-----------dirobj.report_partial_closure () print"----- --------report_full_closure--------------"dirobj.report_full_closure () print" left_list: "+ str (dirobj.left_list) Print "Right_list:" + str (dirobj.right_list) print "Common:" + str (dirobj.common) print "left_only:" + str (dirobj.left_ only) print "right_only:" + str (dirobj.right_only) print "Common_dirs:" + str (dirobj.common_dirs) print "Common_files:" + STR (dirobj.common_files) print "Common_funny:" + str (dirobj.common_funny) print "Same_file:" + str (dirobj.same_files) Print "Diff_files:" + str (dirobj.diff_files) print "Funny_files:" + str (dirobj.funny_files)
Verify source and backup directory differences, and incremental backup to use the FILECMP module left_only, diff_files recursion to get the source directory of the update, and then through the Shutil.copyfile, Os.makedirs method to replicate the update items, and finally maintain a consistent status # !/usr/bin/env python # coding=utf-8 import os, 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 #不匹配文件, the source directory file has changed Dirpath=os.path.abspath (dir1) #定义源目录绝对路径 #将更新文件名或目录追加到holderlist [ Holderlist.append (Os.path.abspath (Os.path.join (dir1,x))) for x in Only_in_one] [Holderlist.append ( Os.path.join (dir1,x))) for x in Diff_in_one] if Len (dircomp.common_dirs) > 0: #判断是否存在相同子目录 so that recursion for item in dircomp.co Mmon_dirs: #递归子目录 compareme (Os.path.abspath (Os.path.join (Dir1,item)), Os.path.abspath (Os.path.join)) # ' \ ' for a newline connector #return holderlist #原代码 return holderlist #修正后, misaligned the function may be returned none, causing the program to error #注意代码对齐, space and tab can not be mixed.
def main (): If Len (SYS.ARGV) > 2: #要求输入源目录和备份目录 dir1=sys.argv[1] dir2=sys.argv[2] else:print "Usage:", sys.argv[0], " DataDir Backupdir "Sys.exit () 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): #如果差异路径为目录且不存在, the If not os.path.exists is created in the backup directory ( Destination_dir): Os.makedirs (destination_dir) createdir_bool=true #再次调用compareme函数标记
If Createdir_bool: #重新调用compareme函数, iterate over the contents of the newly created directory destination_files=[] source_files=[] Source_files=compareme (Dir1, DIR2) #调用compareme函数 for item in Source_files: #获取源目录差异路径清单, corresponding to replace with backup directory 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]): #判断是否为文件, yes copy Operation Shutil.copyfile (Item[0], item[1]) If __name__ = ' __m Ain__ ': Main ()
File content comparison #!/usr/bin/python import difflib Text1 = "" Text1:this module provides classes and functions for comparing SEQUENC Es. including HTML and context and unified diffs. Difflib document v7.4 Add string "" "Text1_lines = Text1.splitlines () Text2 =" "Text2:this module provides classes and F Unctions for comparing sequences. including HTML and context and unified diffs. Difflib document v7.5 "" "Text2_lines = Text2.splitlines () d = difflib. Differ () diff = d.compare (text1_lines, text2_lines) print ' \ n '. Join (List (diff))
Contrast symbol meaning '-' first sequence has, second no ' + ' first sequence none, second has ' two consistent '? ' two sequences exist increment difference ' ^ ' sign out two sequence of difference characters
The Make_file () method using the Htmldiff () class generates beautiful HTML documents the following parts of the above example d = Difflib. Differ () diff = d.compare (text1_lines, text2_lines) print ' \ n '. Join (List (diff)) is replaced with d = difflib. Htmldiff () print d.make_file (text1_lines, text2_lines) file named simple2.py, execute Python simple2.py > diff.html to generate a comparison file
Nginx profile Comparison #!/usr/bin/python # coding=utf-8 import difflib import sys try:textfile1=sys.argv[1] #第一个文件路径参数 textfile2=sy S.ARGV[2] #第二个文件路径参数 except exception,e:

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.