Python實(shí)現(xiàn)比較兩個(gè)文件夾中代碼變化的方法
本文實(shí)例講述了Python實(shí)現(xiàn)比較兩個(gè)文件夾中代碼變化的方法。分享給大家供大家參考。具體如下:
這里將修改代碼后的目錄與原始目錄做對(duì)比,羅列出新增的代碼文件,以及修改過(guò)的代碼文件
# -*- coding: utf-8 -*-
import os;
folderA = "F:\\Projects\\FreeImageV3_14_1\\".lower();
folderB = u"E:\\Software\\圖像解碼庫(kù)\\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();
希望本文所述對(duì)大家的Python程序設(shè)計(jì)有所幫助。
相關(guān)文章
Python基于爬蟲(chóng)實(shí)現(xiàn)全網(wǎng)搜索并下載音樂(lè)
這篇文章主要介紹了Python基于爬蟲(chóng)實(shí)現(xiàn)全網(wǎng)搜索并下載音樂(lè)的方法,幫助大家更好的理解和使用python,感興趣的朋友可以了解下2021-02-02
python機(jī)器學(xué)習(xí)庫(kù)常用匯總
這篇文章主要為大家匯總了常用python機(jī)器學(xué)習(xí)庫(kù),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-11-11
django框架事務(wù)處理小結(jié)【ORM 事務(wù)及raw sql,customize sql 事務(wù)處理】
這篇文章主要介紹了django框架事務(wù)處理,結(jié)合實(shí)例形式總結(jié)分析了使用ORM 事務(wù)及raw sql,customize sql 事務(wù)處理相關(guān)實(shí)現(xiàn)技巧與操作注意事項(xiàng),需要的朋友可以參考下2019-06-06
python cv2截取不規(guī)則區(qū)域圖片實(shí)例
今天小編就為大家分享一篇python cv2截取不規(guī)則區(qū)域圖片實(shí)例,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2019-12-12
python人工智能tensorflow函數(shù)tf.nn.dropout使用方法
這篇文章主要為大家介紹了python人工智能tensorflow函數(shù)tf.nn.dropout使用示例,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2022-05-05

