[Python practice] compares the file differences in two directories and gives a list of differences

Source: Internet
Author: User

Tag:python    practice    

#!/usr/bin/env pythonimport osimport sysdef get_curlist (PATH):         cur_files = os.listdir (PATH)          All_files = []        for file_name in cur_ Files:                full_ Name = os.path.join (Path,file_name)                  all_files.append (full_name)                  if os.path.isdir (Full_Name):                          next_levelfiles = get_curlist (full_name)                          all_files.extend (next_Levelfiles)          return all_Filesitem_Path =  '/usr/local/webapp/ Document ' File_path = get_curlist (item_path) item_newpath =  '/usr/local/webapp/document_ NewDocument ' Newfile_path = get_curlist (item_newpath) def add_file ():         add_List=[]        for num in  Range (0,len (newfile_path)):                 renewfile_path=newfile_path[num].replace (' _newdocument ', ')                  if renewfile_path not in  file_Path:                          add_list.append (Renewfile_path)         return add_listadd_ Filelist = add_file () file_add = open ('/tmp/python_addfile ', ' W ') file_add.truncate () for  num in range (0,len (add_filelist)):         file_add  = open ('/tmp/python_addfile ', ' a ')         file_add.write (Add _filelist[num]+ ' \ n ') file_add.close () Def del_file ():         del_ File=[]        for num in range (0,len (File_Path)):                 refile_path= File_path[num].replace (item_path,item_path+ ' _newdocument ')                  if refile_Path not in newfile_Path:       &nbSp;                 del_ File.append (Refile_path)         return del_Filedel_Filelist  = del_file () file_add = open ('/tmp/python_delfile ', ' W ') file_add.truncate () for num in  range (0,len (del_filelist)):         file_add = open ('/ Tmp/python_delfile ', ' a ')         file_add.write (del_Filelist[num]+ ' \ n ') File_add.close ()

The script is mainly to compare documents and document_newdocument, through replace to modify the obtained file path, compare the file path of another directory to determine whether the file exists on both sides.

The addfile refers to documents that are not in document but in Document_newdocument.

Delfile refers to documents inside the document but not in Document_newdocument.

This article is from the "Empty Heart" blog, please be sure to keep this source http://songknight.blog.51cto.com/2599480/1632808

[Python practice] compares the file differences in two directories and gives a list of differences

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.