Python source directory and backup directory diff check and synchronize

Source: Internet
Author: User
Tags diff

#!/usr/bin/env python# -*- coding: utf-8 -*-import osimport sysimport  Reimport filecmpimport shutilholderlist=[]def compareme (DIR1,DIR2):d Ircomp=filecmp.dircomp (dir1 , Dir2) Only_in_one=dircomp.left_onlydiff_in_one=dircomp.diff_filesdirpath=os.path.abspath (DIR1) [ Holderlist.append (Os.path.abspath (Os.path.join (dir1,x)))  for x in only_in_one][ Holderlist.append (Os.path.abspath (Os.path.join (dir1,x)))  for x 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 holderlistdef main (): If  len (SYS.ARGV)  > 2:dir1=sys.argv[1]dir2=sys.argv[2]else:print  "Please input parameters accurately, e.g.:python  Comp.py 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=falsefor item in source_ Files: #destination_dir =re.sub (Dir1,dir2,item) destination_dir =item.replace (DIR1, DIR2) destination_ Files.append (destination_dir) If os.path.isdir (item): If not os.path.exists (destination_dir): o S.makedirs (Destination_dir) Createdir_bool=trueif createdir_bool:destination_files=[]source_files=[]source _files=compareme (DIR1,DIR2) For item in source_files:destination_dir = item.replace ( DIR1, DIR2) #destination_dir =re.sub (Dir1,dir2,item) destination_files.append (destination_dir) print  " What's NEW: "Print source_filescopy_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__ ': The main () script code can be used directly


Python source directory and backup directory diff check and synchronize

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.