SoFunction
Updated on 2024-10-30

Python implementation to compare code changes in two folders

This article example describes the Python implementation of comparing code changes in two folders. Shared for your reference. Specific as follows:

Here is a comparison of the modified code directory with the original directory, listing the new code files, and the 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 (folderA):
  for fileName in files:
    filePathsA[(root + "\\" + fileName).lower()] = 1;
for root,dirs,files in (folderB):
  for fileName in files:
    filePathsB[(root + "\\" + fileName).lower()] = 1;
# In filePathsA, find the paths of all files that do not match those in filePathsB
modifiedFilePath = [];
addedFilePath = [];
for filePathA in filePathsA:
  folderALen = len(folderA);
  filePathB = folderB + filePathA[folderALen:]; 
  idx = (".");
  if idx == -1:
    continue;
  ext = filePathA[idx + 1:];
  ext = ();
  if ext != "c" and ext != "h" and ext != "cpp" and ext != "cxx":
    continue;
  if filePathB not in filePathsB:
    (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:   
    (filePathA);
output = open('', 'w');
("added files:\n");
for filePath in addedFilePath:
  (filePath + "\n");
("modified files:\n");
for filePath in modifiedFilePath:
  (filePath + "\n");
();

I hope that what I have described in this article will help you in your Python programming.