Python实现比较两个文件夹中代码变化的方法

yipeiwu_com5年前Python基础

本文实例讲述了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程序设计有所帮助。

相关文章

python发送告警邮件脚本

python发送告警邮件脚本

python脚本为敏捷开发脚本,在zabbix监控也起到重要作用,以下是使用python脚本发送告警邮件配置方法。 脚本如下: #!/usr/bin/python #coding:u...

python3用PIL把图片转换为RGB图片的实例

感想 我们在做深度学习处理图片的时候,如果是自己制作或者收集的数据集,不可避免的要对数据集进行处理,然后大多数模型都只支持RGB格式的图片,这个时候,我们需要把其他格式的图片,例如灰度图...

python进阶教程之词典、字典、dict

基础教程介绍了基本概念,特别是对象和类。 进阶教程对基础教程的进一步拓展,说明Python的细节。希望在进阶教程之后,你对Python有一个更全面的认识。 之前我们说了,列表是Pytho...

Django自带的加密算法及加密模块详解

Django 内置的User类提供了用户密码的存储、验证、修改等功能,可以很方便你的给用户提供密码服务。 默认的Ddjango使用pbkdf2_sha256方式来存储和管理用的密码,当然...

在Python中合并字典模块ChainMap的隐藏坑【推荐】

在Python中合并字典模块ChainMap的隐藏坑【推荐】

在Python中,当我们有两个字典需要合并的时候,可以使用字典的 update 方法,例如: a = {'a': 1, 'b': 2} b = {'x': 3, 'y': 4} a....