Python implementation to compare the code changes in the two folders

Source: Internet
Author: User
This article mainly introduces how to compare the code changes in two folders in Python. The example analyzes the skills related to reading files and string operations in a folder in Python, which has some reference value, for more information about how to compare code changes in two folders in Python, see the example in this article. Share it with you for your reference. The details are as follows:

The modified code directory is compared with the original directory to list the new code files and modified code files.

#-*-Coding: UTF-8-*-import OS; folderA = "F: \ Projects \ FreeImageV3_14_1 \\". lower (); folderB = u "E: \ Software \ image decoding Library \ FreeImage3141 \ FreeImage \\". lower (); filePathsA ={}; filePathsB ={}; for root, dirs, files in OS. walk (folderA): for fileName in files: filePathsA [(root + "\" + fileName ). lower ()] = 1; for root, dirs, files in OS. walk (folderB): for fileName in files: filePathsB [(root + "\" + fileName ). l Ower ()] = 1; # in filePathsA, find the path modifiedFilePath = []; addedFilePath = []; for filePathA in filePathsA for all files inconsistent with filePathsB: folderALen = len (folderA); filePathB = folderB + filePathA [folderALen:]; idx = filePathA. rfind (". "); if idx =-1: continue; ext = filePathA [idx + 1:]; ext = ext. lower (); if ext! = "C" and ext! = "H" and ext! = "Cpp" and ext! = "Cxx": continue; if filePathB not in filePathsB: addedFilePath. append (filePathA); continue; text_file = open (filePathA, "r"); textA = text_file.read (); text_file.close (); text_file = open (filePathB, "r "); textB = text_file.read (); text_file.close (); if textA! = TextB: modifiedFilePath. append (filePathA); output = open('res.txt ', 'w'); output. write ("added files: \ n"); for filePath in addedFilePath: output. write (filePath + "\ n"); output. write ("modified files: \ n"); for filePath in modifiedFilePath: output. write (filePath + "\ n"); output. close ();

I hope this article will help you with Python programming.

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.