Simple use of filecmp in Python

Source: Internet
Author: User
Tags file diff

The FILECMP module is used to compare the contents of files and folders, and it is a lightweight tool that is very simple to use. The Python standard library also provides the contents of the Difflib module for comparing files. About the Difflib module, and listen to tell.

FILECMP defines two functions that allow you to easily compare files and folders:

FILECMP.CMP (F1, f2[, shallow]):

Compares the contents of two files for a match. Parameter F1, F2 specifies the path of the file to compare. The optional parameter shallow specifies whether you need to consider the properties of the file itself (through the Os.stat function to get the file attributes) when comparing files. If the contents of the file match, the function returns True, otherwise false is returned.

Filecmp.cmpfiles (Dir1, Dir2, common[, Shallow]):

Compares the specified files within two folders for equality. Parameter Dir1, DIR2 specifies the folder to compare, and the parameter common specifies the list of file names to compare. The function returns a tuple that contains 3 list elements, representing a list of matching, mismatched, and incorrect files, respectively. The wrong file refers to a nonexistent file, or the file is not readable, or does not have permission to read the file, or it cannot be accessed for any other reason.

A dircmp class is defined in the FILECMP module to compare folders, and by comparing two folders, you can get some detailed comparison results (such as a list of files that exist only in the A folder) and support recursive comparisons of subfolders.

2. Simple use of filecmp

simple use of 2.1 cmp

usage : filecmp.cmp (file1,file2), if File1 and File2 are the same, returns true, otherwise false, which is referred to as the diff of the single file.

2.1.1 Copy A file backup two times

1 # cp/etc/vnc.conf./2 # cp/etc/vnc.conf./vnc.conf.bak

2.1.2 writing python code

1 # Cat lcmp.py 2  3 #!/usr/bin/env python 4  5 import sys 6  7 import filecmp 8  9 import os10 one try:12 13
  file1 = sys.argv[1]14     file2 = sys.argv[2]16 except:18     print ("Please follow the parameters")     Sys.exit () Os.path.isfile (File1) and Os.path.isfile (file2):     if FILECMP.CMP (file1,file2):         Print ("match success")     else:30         print ("Match failed") else:34 print ("Please     check files" ) Notoginseng     Sys.exit ()

2.1.2 executing script output

1 # python lcmp.py vnc.conf vnc.conf.bak 2 Match success

from the appeal results can be seen , the file is OK , now modify the contents of Vnc.conf.bak, and then execute the script

2.1.3 re-execute

1 # sed-i S/vnc/liwang.org/vnc.conf.bak2 # python lcmp.py vnc.conf Vnc.conf.bak 3 Match failed

The file is not successful, it outputs the Match failed, it proves that the script is OK

simple use of 2.2 cmpfiles

usage : Filecmp.cmpfiles (Dir1,dir2,common[files ...]), the function is to compare DIR1 and Dir2 directory differences, the method will return three lists, respectively, matching, mismatch, error.

2.2.1 Copying Files

1 # mkdir-p Dir1 dir22 # cp lcmp.py vnc.conf vnc.conf.bak DIR1/3 # cp lcmp.py vnc.conf dir2/

2.2.2 writing python code

 1 # cat lcmpfiles.py 2 3 #!/usr/bin/env Python 4 5 import OS 6 7 import filecmp 8  9 Import sys10 One dir1 = input ("Please enter a folder to match:") Dir2 = input ("Please enter a folder to match:") 14      Files = []16 true:18 local_files = input ("Please enter the file to compare:[n/n Exit the input]") 20 21 if local_files = = ' n ' or local_files = = ' n ': break24 elif local_files = = ': Continu E28 else:30 to Files.append (local_files), try:34 (os.path.exists), Notoginseng Dir1 (d IR2) except:40 print ("Pleae check the folder.") Sys.exit () #print (Filecmp.cmpfiles (Dir1,dir2,files) [0]), print ("It's File match:", Filecmp.cmpfiles (di  R1,dir2,files) [0]) ("The file does not match:", Filecmp.cmpfiles (Dir1,dir2,files) [1]) print ("file does not exists: ", Filecmp.cmpfiles (Dir1,dir2,files) [2]) 

2.2.3 Executing the script with python3 ( because input is used)

1 # python3 lcmpfiles.py  2 Please enter a folder to Match:dir1 3 Please enter a folder to Match:dir2 4 Please enter th E file to compare:[n/n exit the input]lcmp.py 5 Please enter the file to compare:[n/n exit the input]vnc.conf 6 please ENT Er the file to compare:[n/n exit the Input]vnc.conf.bak 7 Please enter the file to compare:[n/n exit the Input]n 8 It ' s fi Le match: [' lcmp.py ', ' vnc.conf '] 9 The file does not match: []10 file does not exists: [' Vnc.conf.bak ']

As can be seen,lcmp.py and vnc.conf in Dir1 and DR2 have, and file content is the same, and Vnc.conf.bak in Dir1 have, dir No, therefore output, file matching: lcmp.py and vnc.conf, file does not exist: Vnc.conf.bak, file is not the same: none

simple use of 2.2 dircmp

Syntax :d ircmp (a,b,[,ignore[,hide]) where A, b is the file name,Ignore is a list that can be ignored, Hide represents a hidden list , dircmp you can get more detailed information about the directory and also support recursion.

The DIRCMP provides three output methods :

Report () compares content in the current specified directory

Report_full_closure () recursively compares the contents of all specified files

2.2.1 Simulation Environment

1 # ls DIR1/DIR2/2 dir1/:3 hosts  ld.so.conf  sysconfig4 5 dir2/:6 hosts  ld.so.conf  sysconfig

where sysconfig is a directory hosts and ld.so.conf are files , and hosts Inconsistent content Sysconfig is not the same as the files in

2.2.2 writing python code

2.2.2.1 Dircmp.report ()

1 # Cat simple_filecmp.py 2  3 #!/usr/bin/env python 4  5 import filecmp 6  7 Dir1 = "/root/python/d_2_filecmp/cm P/dir2 "8  9 Dir2 ="/root/python/d_2_filecmp/cmp/dir1 ", DirObj = filecmp.dircmp (dir1,dir2) print (dirobj.rep ORT ())

2.2.2.2 Execute Script

1 # python simple_filecmp.py 2 diff/root/python/d_2_filecmp/cmp/dir2/root/python/d_2_filecmp/cmp/dir13 Identical Files: [' ld.so.conf ']4 differing files: [' hosts ']5 Common subdirectories: [' sysconfig ']6 None7 [root@localhost cmp]# CA T simple_filecmp.py

from the above results, we can see thatthe report can only match the first-level directory of the script , but not the directory under the sub-folder

2.2.2.3 Report_full_closure ()

1 # Cat simple_filecmp_2.py 2  3 #!/usr/bin/env python 4  5 import filecmp 6  7 Dir1 = "/root/python/d_2_filecmp/ cmp/dir1/"8  9 Dir2 ="/root/python/d_2_filecmp/cmp/dir2/", DirObj = filecmp.dircmp (dir1,dir2) print (dirobj . Report_full_closure ())

2.2.2.4 Execute Script

1 diff/root/python/d_2_filecmp/cmp/dir1//ROOT/PYTHON/D_2_FILECMP/CMP/DIR2/2 identical files: [' ld.so.conf ']3 Differing files: [' hosts ']4 Common subdirectories: [' Sysconfig ']5 6 diff/root/python/d_2_filecmp/cmp/dir1/sysconfig/ Root/python/d_2_filecmp/cmp/dir2/sysconfig7 ...

The difference between the report () and the report_full_closure () is thus

3.filecmp Case

3.1 Demand

Requirements: 1. Back up all content under the ETC folder , and keep a real-time backup , If there is a new file, Copy to the backup file, if there is a new one, the Update of

3.2 Flowchart

3.2.1 Preliminary Flowchart :

3.2.2 Contrast file diff flowchart

3.3 Code writing :

3.3.1 Supplemental Knowledge :

Dircmp.left_only

Files that appear only on the left

1 # Cat simple_filecmp_3.py 2  3 #!/usr/bin/env python 4  5 import filecmp 6  7 Dir1 = "/root/python/d_2_filecmp/ cmp/dir1/"8  9 Dir2 ="/root/python/d_2_filecmp/cmp/dir2/", DirObj = filecmp.dircmp (dir1,dir2) print (Dirob J.diff_files)

Execution results

1 # ls Dir1 DIR2/2 dir1:3 hosts  ld.so.conf  sysconfig  teacher4 5 dir2/:6 hosts  ld.so.conf  Sysconfig7 [Root@localhost cmp]# python simple_filecmp_3.py 8 [' Teacher ']

It can be seen from the appeal that when teacher only appears in dir1 , It is crawled out , the so-called Left and the Right is relative to filecmp.dircmp in terms of

Dircmp.diff_files

Returns no quota file

1 # Cat simple_filecmp_3.py 2  3 #!/usr/bin/env python 4  5 import filecmp 6  7 Dir1 = "/root/python/d_2_filecmp/ cmp/dir1/"8  9 Dir2 ="/root/python/d_2_filecmp/cmp/dir2/", DirObj = filecmp.dircmp (dir1,dir2) print (dirobj . Diff_files) #print (dirobj.left_only)

Execution results

1 [root@localhost cmp]# ls dir1 dir22 dir1:3 hosts  ld.so.conf  sysconfig  teacher4 5 Dir2:6 hosts  ld.so.conf  Sysconfig7 [root@localhost cmp]# python simple_filecmp_3.py 8 [' Hosts ']9 [root@localhost cmp]#

before we modified the hosts file, the file content has been inconsistent, has now been crawled out

3.3.2 Writing an automated backup script

 1 # cat d_7_12_filecmp.py 2 #!/usr/bin/env Python 3 4 import filecmp 5 import OS 6 import sys 7 import shutil 8 9 Sour Ce_files = "/root/python/d_2_filecmp/dir1" target_files = "/root/python/d_2_filecmp/dir2" one-off def check_common_dirs (source_files,target_files): Dirsobj = filecmp.dircmp (Source_files, target_files) common_dirs_list = Dirso BJ.COMMON_DIRS16 Common_line in Common_dirs_list:18 files_contrast ('/' +source_files+ '/' +common_lin     E, '/' +target_files+ '/' +common_line ') def files_contrast (DIR1,DIR2): DirObj = filecmp.dircmp (DIR1,DIR2) 23 24 No_exists_files = dirobj.left_only25 No_diff_files = dirobj.diff_files26 to Exists_files in No_exists_file s:28 if Os.path.isfile (exists_files): Shutil.copyfile ('/' +dir1+ '/' +exists_files, '/' + dir2+ '/' +exists_files) else:32 print ("%s is dirctory"% (exists_files)) Os.makedirs (' /' +dir2+ '/' +exists_files) 34            Print ("%s is mkdirs"% ('/' +target_files+ '/' +exists_files) ") try:37 Print ("Values:%s"% ('/' +dir1+ '/' +exists_files, '/' +dir2+ '/' +exists_files) ') files_contrast ('/' + dir1+ '/' +exists_files, '/' +dir2+ '/' +exists_files ', ' except:40 return ' for Diff_file             s in no_diff_files:43 if Os.path.isfile (diff_files): Os.remove ('/' +dir2+ '/' +diff_files) 45  Shutil.copyfile ('/' +dir1+ '/' +diff_files, '/' +dir2+ '/' +diff_files ') if Os.path.exists (source_files): Os.path.exists (target_files) = = "False": Os.makedirs (Target_files) files_contrast (source_files,ta     Rget_files) Check_common_dirs (source_files,target_files) else:56 print ("Soure files no exists") 57 Sys.exit ()

3.4 executing script output

3.4.1 Viewing Files

There's no file under Dir2.

1 # Tree DIR1/DIR2/2 dir1/3├──123 4│   └──123456 5├──4556 6│   └──789 7│       └──d 8├──lcmp.py 9├──vnc . conf10└──vnc.conf.bak11 DIR2/12 3 directories, 5 files

3.4.2 Execute Script

 1 root@localhost d_2_filecmp]# python d_7_12_filecmp.py 2 4556 is dirctory 3//root /python/d_2_filecmp/dir2/4556 is mkdirs 4 values://root/python/d_2_filecmp/dir1/4556//ROOT/PYTHON/D_2_FILECMP/DIR2  /4556 5 789 is dirctory 6//root/python/d_2_filecmp/dir2/789 is mkdirs 7 values:///root/python/d_2_filecmp/dir1/4556/789 root/python/d_2_filecmp/dir2/4556/789 8 D is dirctory 9//root/python/d_2_filecmp/dir2/d is MKDIRS10 values:////root /PYTHON/D_2_FILECMP/DIR1/4556/789/D////root/python/d_2_filecmp/dir2/4556/789/d11 123 is Dirctory12//root/python/d _2_filecmp/dir2/123 is MKDIRS13 values://root/python/d_2_filecmp/dir1/123//root/python/d_2_filecmp/dir2/12314 123456 is DIRCTORY15//root/python/d_2_filecmp/dir2/123456 is MKDIRS16 values:///root/python/d_2_filecmp/dir1/123/ 123456///root/python/d_2_filecmp/dir2/123/123456 

As can be seen , the backup information , in front of multiple / can be disregarded,Linux only recognizes a /

3.4.3 viewing backup effects

1 # Tree DIR1/DIR2/2 dir1/3├──123 4│   └──123456 5├──4556 6│   └──789 7│       └──d 8├──lcmp.py 9├── Vnc.conf10└──vnc.conf.bak11 dir2/12├──12313│   └──12345614├──455615│   └──78916│       └──d17├──lcmp.py 18├──VNC.CONF19└──VNC.CONF.BAK20 8 directories, 8 files

from the top, you know , backup is completely successful, for timing execution python script script can be written to crontab , open the scheduled task.

Related Article

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.