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.