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构建Hopfield网络的教程
Apr 14 Python
Python中index()和seek()的用法(详解)
Apr 27 Python
老生常谈Python序列化和反序列化
Jun 28 Python
python: 判断tuple、list、dict是否为空的方法
Oct 22 Python
python实现写数字文件名的递增保存文件方法
Oct 25 Python
老生常谈python中的重载
Nov 11 Python
PyQt5 实现字体大小自适应分辨率的方法
Jun 18 Python
python快速编写单行注释多行注释的方法
Jul 31 Python
Python集合基本概念与相关操作实例分析
Oct 30 Python
python boto和boto3操作bucket的示例
Oct 30 Python
Python 可视化神器Plotly详解
Dec 26 Python
selenium+headless chrome爬虫的实现示例
Jan 08 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(3) php 函数
2010/02/15 PHP
php学习笔记之基础知识
2014/11/08 PHP
php5.4以下版本json不支持不转义内容中文的解决方法
2015/01/13 PHP
php基于curl实现的股票信息查询类实例
2016/11/11 PHP
PHP实现微信JS-SDK接口选择相册及拍照并上传的方法
2016/12/05 PHP
自制PHP框架之设计模式
2017/05/07 PHP
Yii框架getter与setter方法功能与用法分析
2019/10/22 PHP
Laravel5.5+ 使用API Resources快速输出自定义JSON方法详解
2020/04/06 PHP
textarea的value是html文件源代码,存成html文件的代码
2007/04/20 Javascript
jquery 之 $().hover(func1, funct2)使用方法
2012/06/14 Javascript
Javascript实现动态菜单添加的实例代码
2013/07/05 Javascript
javascript实现简单的省市区三级联动
2015/05/14 Javascript
AngularJS过滤器详解及示例代码
2016/08/16 Javascript
浅谈JS读取DOM对象(标签)的自定义属性
2016/11/21 Javascript
BootstrapTable加载按钮功能实例代码详解
2017/09/22 Javascript
微信小程序实现图片上传功能实例(前端+PHP后端)
2018/01/10 Javascript
搭建element-ui的Vue前端工程操作实例
2018/02/23 Javascript
ionic3双击返回退出应用的方法
2019/09/17 Javascript
JS代码检查工具ESLint介绍与使用方法
2020/02/04 Javascript
在vue中实现嵌套页面(iframe)
2020/07/30 Javascript
[35:44]2014 DOTA2华西杯精英邀请赛 5 24 iG VS VG
2014/05/26 DOTA
详解Python文本操作相关模块
2017/06/22 Python
Python使用selenium实现网页用户名 密码 验证码自动登录功能
2018/05/16 Python
python视频按帧截取图片工具
2019/07/23 Python
Django实现从数据库中获取到的数据转换为dict
2020/03/27 Python
Joseph官网:英国小众奢侈品牌
2019/05/17 全球购物
Lookfantastic阿联酋官网:英国知名美妆护肤购物网站
2020/05/26 全球购物
华为消费者德国官方网站:HUAWEI德国
2020/11/03 全球购物
自我评价怎么写好呢?
2013/12/05 职场文书
《狼和小羊》教学反思
2014/04/20 职场文书
办公室班子四风问题对照检查材料
2014/10/04 职场文书
辞职信怎么写
2015/02/27 职场文书
增值税发票丢失证明
2015/06/19 职场文书
教师法制教育培训学习心得体会
2016/01/14 职场文书
民事纠纷协议书
2016/03/23 职场文书
浅谈MySQL之浅入深出页原理
2021/06/23 MySQL