Python实现比较两个文件夹中代码变化的方法
作者:speedmancs 时间:2022-04-07 23:16:08
本文实例讲述了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,比较,文件夹
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
python 实现体质指数BMI计算
2023-01-25 12:44:56
![](https://img.aspxhome.com/file/2023/1/110571_0s.png)
浅谈Python函数式编程的返回函数与匿名函数
2021-02-22 13:40:36
对Python使用mfcc的两种方式详解
2023-06-04 14:56:52
![](https://img.aspxhome.com/file/2023/0/67810_0s.jpg)
Python编程中运用闭包时所需要注意的一些地方
2021-10-27 06:07:21
国外新闻报纸排版布局设计欣赏
2008-02-25 23:14:00
![](https://img.aspxhome.com/file/UploadPic/20082/25/2008225234113305s.jpg)
Python编程产生非均匀随机数的几种方法代码分享
2023-02-10 02:00:19
![](https://img.aspxhome.com/file/2023/7/67417_0s.png)
asp 采集程序常用函数分析
2011-03-16 11:03:00
Dreamweaver MX弹出窗口全攻略
2010-09-05 21:14:00
![](https://img.aspxhome.com/file/UploadPic/20072/20072311313962s.jpg)
Flask之请求钩子的实现
2023-06-27 10:43:59
解决pycharm的Python console不能调试当前程序的问题
2021-09-08 12:02:42
Python threading中lock的使用详解
2023-01-16 08:32:26
Python语言实现二分法查找
2021-12-01 18:39:49
![](https://img.aspxhome.com/file/2023/5/85375_0s.png)
PHP echo()函数讲解
2023-06-05 18:50:54
![](https://img.aspxhome.com/file/2023/0/55390_0s.png)
Python中的字典合并与列表合并技巧
2021-01-02 06:07:35
django自带的server 让外网主机访问方法
2023-06-03 22:59:50
海王小姐姐悄悄问我怎么在PC端登录多个微信
2021-10-27 16:25:10
![](https://img.aspxhome.com/file/2023/8/77818_0s.jpg)
python数据类型强制转换实例详解
2022-02-08 05:22:43
asp xmlhttp下载一句话
2013-06-30 06:47:48
mysql Load Data InFile 的用法
2009-09-06 12:08:00
PyTorch 如何自动计算梯度
2023-08-13 14:44:20
![](https://img.aspxhome.com/file/2023/7/59837_0s.jpg)