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(一)Python环境安装
Aug 20 Python
Python中3种内建数据结构:列表、元组和字典
Nov 30 Python
python检查序列seq是否含有aset中项的方法
Jun 30 Python
在CentOS6上安装Python2.7的解决方法
Jan 09 Python
Python排序算法之选择排序定义与用法示例
Apr 29 Python
Sanic框架流式传输操作示例
Jul 18 Python
Django中的Model操作表的实现
Jul 24 Python
python递归实现快速排序
Aug 18 Python
python实现凯撒密码、凯撒加解密算法
Jun 11 Python
python基础学习之生成器与文件系统知识总结
May 25 Python
Python代码风格与编程习惯重要吗?
Jun 03 Python
pd.drop_duplicates删除重复行的方法实现
Jun 16 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
Syphon 虹吸式咖啡壶冲煮–拨动法
2021/03/03 冲泡冲煮
深入PHP中的HashTable结构详解
2013/06/13 PHP
浅析php变量作用域的一些问题
2013/08/08 PHP
php获取汉字拼音首字母的方法
2015/10/21 PHP
PHP+mysql+ajax轻量级聊天室实现方法详解
2016/10/17 PHP
探讨javascript是不是面向对象的语言
2013/11/21 Javascript
在javascript中执行任意html代码的方法示例解读
2013/12/25 Javascript
javascript常见操作汇总
2014/09/03 Javascript
学习vue.js中class与style绑定
2016/12/03 Javascript
基于vue.js中事件修饰符.self的用法(详解)
2018/02/23 Javascript
vue.extend实现alert模态框弹窗组件
2018/04/28 Javascript
详解使用VueJS开发项目中的兼容问题
2018/08/02 Javascript
Vue数据双向绑定的深入探究
2018/11/27 Javascript
jquery实现选项卡切换代码实例
2019/05/14 jQuery
json_decode 索引为数字时自动排序问题解决方法
2020/03/28 Javascript
Vue+scss白天和夜间模式切换功能的实现方法
2021/01/05 Vue.js
Python SQLAlchemy基本操作和常用技巧(包含大量实例,非常好)
2014/05/06 Python
利用Python的装饰器解决Bottle框架中用户验证问题
2015/04/24 Python
用Python计算三角函数之acos()方法的使用
2015/05/15 Python
初步认识Python中的列表与位运算符
2015/10/12 Python
关于Tensorflow中的tf.train.batch函数的使用
2018/04/24 Python
Python基于property实现类的特性操作示例
2018/06/15 Python
利用PyCharm操作Github(仓库新建、更新,代码回滚)
2019/12/18 Python
浅谈Python中的异常和JSON读写数据的实现
2020/02/27 Python
Python Scrapy框架:通用爬虫之CrawlSpider用法简单示例
2020/04/11 Python
keras实现调用自己训练的模型,并去掉全连接层
2020/06/09 Python
python 爬取百度文库并下载(免费文章限定)
2020/12/04 Python
python 调用Google翻译接口的方法
2020/12/09 Python
美国创意礼品网站:UncommonGoods
2017/02/03 全球购物
农田水利实习自我鉴定
2013/09/19 职场文书
护理学毕业生自荐信
2013/10/02 职场文书
儿科护士实习自我鉴定
2013/10/17 职场文书
ktv总经理岗位职责
2014/02/17 职场文书
作风转变年心得体会
2014/10/22 职场文书
2015年社区关工委工作总结
2015/04/03 职场文书
2016年教师政治思想表现评语
2015/12/02 职场文书