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中文乱码的解决方法
Nov 04 Python
python中的__init__ 、__new__、__call__小结
Apr 25 Python
Python实现冒泡,插入,选择排序简单实例
Aug 18 Python
Python中使用PyQt把网页转换成PDF操作代码实例
Apr 23 Python
python版简单工厂模式
Oct 16 Python
Python数据结构与算法之使用队列解决小猫钓鱼问题
Dec 14 Python
python实现学生管理系统
Jan 11 Python
python实现拓扑排序的基本教程
Mar 11 Python
python json.loads兼容单引号数据的方法
Dec 19 Python
Python中asyncio模块的深入讲解
Jun 10 Python
python读取图片的几种方式及图像宽和高的存储顺序
Feb 11 Python
关于pycharm 切换 python3.9 报错 ‘HTMLParser‘ object has no attribute ‘unescape‘ 的问题
Nov 24 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
MayFish PHP的MVC架构的开发框架
2009/08/13 PHP
php用户注册页面利用js进行表单验证具体实例
2013/10/17 PHP
phpQuery占用内存过多的处理方法
2013/11/13 PHP
PHP+swoole实现简单多人在线聊天群发
2016/01/19 PHP
发布一个高效的JavaScript分析、压缩工具 JavaScript Analyser
2007/11/30 Javascript
js不能获取隐藏的div的宽度只能先显示后获取
2014/09/04 Javascript
js仿土豆网带缩略图的焦点图片切换效果实现方法
2015/02/23 Javascript
Jquery实现瀑布流布局(备有详细注释)
2015/07/31 Javascript
JS实现随页面滚动显示/隐藏窗口固定位置元素
2016/02/26 Javascript
JavaScript比较两个数组的内容是否相同(推荐)
2017/05/02 Javascript
jQuery中的deferred对象和extend方法详解
2017/05/08 jQuery
Node.JS利用PhantomJs抓取网页入门教程
2017/05/19 Javascript
浅谈实现vue2.0响应式的基本思路
2018/02/13 Javascript
vue element-ui table表格滚动加载方法
2018/03/02 Javascript
three.js搭建室内场景教程
2018/12/30 Javascript
3分钟了解vue数据劫持的原理实现
2019/05/01 Javascript
antd日期选择器禁止选择当天之前的时间操作
2020/10/29 Javascript
vue图片裁剪插件vue-cropper使用方法详解
2020/12/16 Vue.js
vue 在单页面应用里使用二级套嵌路由
2020/12/19 Vue.js
[01:03:09]完美世界DOTA2联赛PWL S2 Forest vs SZ 第二场 11.25
2020/11/26 DOTA
Python实现分割文件及合并文件的方法
2015/07/10 Python
详解Python的collections模块中的deque双端队列结构
2016/07/07 Python
python使用 HTMLTestRunner.py生成测试报告
2017/10/20 Python
python正则表达式之对号入座篇
2018/07/24 Python
Python制作微信好友背景墙教程(附完整代码)
2019/07/17 Python
浅谈Html5多线程开发之WebWorkers
2018/05/02 HTML / CSS
中粮集团旗下食品网上购物网站:我买网
2016/09/22 全球购物
爱尔兰电脑、家电和家具购物网站:Buy It Direct
2019/07/09 全球购物
校园招聘策划书
2014/01/09 职场文书
捐书寄语赠言
2014/01/18 职场文书
会计电算化专业求职信
2014/06/10 职场文书
标准毕业生自荐信
2014/06/24 职场文书
应届毕业生求职信范文
2014/07/07 职场文书
python 命令行传参方法总结
2021/05/25 Python
笔记本自带的win11如何跳过联网激活?
2022/04/20 数码科技
MySQL索引失效十种场景与优化方案
2023/05/08 MySQL