Python實現比較兩個檔案夾中代碼變化的方法

來源:互聯網
上載者:User
本文執行個體講述了Python實現比較兩個檔案夾中代碼變化的方法。分享給大家供大家參考。具體如下:

這裡將修改代碼後的目錄與原始目錄做對比,羅列出新增的代碼檔案,以及修改過的代碼檔案

# -*- coding: utf-8 -*-import os;folderA = "F:\\Projects\\FreeImageV3_14_1\\".lower();folderB = u"E:\\Software\\映像解碼庫\\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).lower()] = 1;# 在filePathsA中,找到所有和filePathsB中不一致的檔案的路徑    modifiedFilePath = [];addedFilePath = [];for filePathA in filePathsA:  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();

希望本文所述對大家的Python程式設計有所協助。

  • 聯繫我們

    該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

    如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

    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.