本文实例讲述了python实现比较两个文件夹中代码变化的方法。分享给大家供大家参考。具体如下:
这里将修改代码后的目录与原始目录做对比,罗列出新增的代码文件,以及修改过的代码文件
# -*- coding: utf-8 -*-import os;foldera = f:\\projects\\freeimagev3_14_1\\.lower();folderb = ue:\\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程序设计有所帮助。
