[Python] 比较两个文件夹中的代码变化 →→→→→进入此内容的聊天室

来自 , 2019-04-03, 写在 Python, 查看 119 次.
URL http://www.code666.cn/view/1e747ddb
  1. # -*- coding: utf-8 -*-
  2. import os;
  3.  
  4. folderA = "F:\\Projects\\FreeImageV3_14_1\\".lower();
  5. folderB = u"E:\\Software\\图像解码库\\FreeImage3141\\FreeImage\\".lower();
  6.  
  7. filePathsA = {};
  8. filePathsB = {};
  9. for root,dirs,files in os.walk(folderA):
  10.         for fileName in files:
  11.                 filePathsA[(root + "\\" + fileName).lower()] = 1;
  12.  
  13. for root,dirs,files in os.walk(folderB):
  14.         for fileName in files:
  15.                 filePathsB[(root + "\\" + fileName).lower()] = 1;
  16.                
  17. # 在filePathsA中,找到所有和filePathsB中不一致的文件的路径           
  18. modifiedFilePath = [];
  19. addedFilePath = [];
  20. for filePathA in filePathsA:
  21.  
  22.         folderALen = len(folderA);
  23.         filePathB = folderB + filePathA[folderALen:];
  24.        
  25.         idx = filePathA.rfind(".");
  26.         if idx == -1:
  27.                 continue;
  28.         ext = filePathA[idx + 1:];
  29.         ext = ext.lower();
  30.         if ext != "c" and ext != "h" and ext != "cpp" and ext != "cxx":
  31.                 continue;
  32.         if filePathB not in filePathsB:
  33.                 addedFilePath.append(filePathA);
  34.                 continue;
  35.  
  36.         text_file = open(filePathA, "r");
  37.         textA = text_file.read();
  38.         text_file.close();
  39.        
  40.         text_file = open(filePathB, "r");
  41.         textB = text_file.read();
  42.         text_file.close();
  43.        
  44.         if textA != textB:             
  45.                 modifiedFilePath.append(filePathA);
  46.  
  47. output = open('res.txt', 'w');
  48. output.write("added files:\n");
  49. for filePath in addedFilePath:
  50.         output.write(filePath + "\n");
  51.  
  52. output.write("modified files:\n");
  53. for filePath in modifiedFilePath:
  54.         output.write(filePath + "\n");
  55. output.close();
  56. #//python/1185

回复 "比较两个文件夹中的代码变化"

这儿你可以回复上面这条便签

captcha