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


Posted in Python onJuly 10, 2015

本文实例讲述了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发送Email方法实例
Aug 21 Python
简单介绍Python中利用生成器实现的并发编程
May 04 Python
python 打印出所有的对象/模块的属性(实例代码)
Sep 11 Python
python解决网站的反爬虫策略总结
Oct 26 Python
Python多层装饰器用法实例分析
Feb 09 Python
python2 与 pyhton3的输入语句写法小结
Sep 10 Python
从运行效率与开发效率比较Python和C++
Dec 14 Python
python删除文件夹下相同文件和无法打开的图片
Jul 16 Python
python并发编程多进程 模拟抢票实现过程
Aug 20 Python
使用TensorFlow对图像进行随机旋转的实现示例
Jan 20 Python
python 解决函数返回return的问题
Dec 05 Python
Anaconda安装pytorch和paddle的方法步骤
Apr 03 Python
python简单文本处理的方法
Jul 10 #Python
Python实现把json格式转换成文本或sql文件
Jul 10 #Python
Python中的一些陷阱与技巧小结
Jul 10 #Python
Python中的fileinput模块的简单实用示例
Jul 09 #Python
Python中的anydbm模版和shelve模版使用指南
Jul 09 #Python
python冒泡排序简单实现方法
Jul 09 #Python
python基于BeautifulSoup实现抓取网页指定内容的方法
Jul 09 #Python
You might like
让你的PHP同时支持GIF、png、JPEG
2006/10/09 PHP
php一些错误处理的方法与技巧总结
2013/08/10 PHP
ThinkPHP中公共函数路径和配置项路径的映射分析
2014/11/22 PHP
ecshop后台编辑器替换成ueditor编辑器
2015/03/03 PHP
Laravel 验证码认证学习记录小结
2019/12/20 PHP
Jquery中获取iframe的代码
2011/01/11 Javascript
jquery实现导航固定顶部的效果仿蘑菇街
2014/10/22 Javascript
不得不看之JavaScript构造函数及new运算符
2017/08/21 Javascript
一步步教你利用webpack如何搭一个vue脚手架(超详细讲解和注释)
2018/01/08 Javascript
vue非父子组件通信问题及解决方法
2018/06/11 Javascript
vue.js template模板的使用(仿饿了么布局)
2018/08/13 Javascript
微信小程序时间轴实现方法示例
2019/01/14 Javascript
[03:17]2016完美“圣”典风云人物:冷冷专访
2016/12/08 DOTA
Python使用logging模块实现打印log到指定文件的方法
2018/09/05 Python
用于业余项目的8个优秀Python库
2018/09/21 Python
python twilio模块实现发送手机短信功能
2019/08/02 Python
Django 拆分model和view的实现方法
2019/08/16 Python
Lombok插件安装(IDEA)及配置jar包使用详解
2020/11/04 Python
Python使用grequests并发发送请求的示例
2020/11/05 Python
使用html5 canvas 画时钟代码实例分享
2015/11/11 HTML / CSS
Bulk Powders意大利:运动补充在线商店
2019/02/09 全球购物
接口的多继承会带来哪些问题
2015/08/17 面试题
不同浏览器创建XMLHttpRequest方法有什么不同
2014/11/17 面试题
分公司经理岗位职责
2013/11/11 职场文书
学生就业推荐信
2013/11/13 职场文书
英语老师推荐信
2014/02/26 职场文书
幼教求职信
2014/03/12 职场文书
竞选学习委员演讲稿
2014/04/28 职场文书
教育基金募捐倡议书
2014/05/14 职场文书
市级三好学生事迹材料
2014/08/27 职场文书
国庆节促销广告语2014
2014/09/19 职场文书
民事和解协议书格式
2014/11/29 职场文书
Pytorch 统计模型参数量的操作 param.numel()
2021/05/13 Python
python使用glob检索文件的操作
2021/05/20 Python
5分钟教你docker安装启动redis全教程(全新方式)
2021/05/29 Redis
详解nginx安装过程并代理下载服务器文件
2022/02/12 Servers