比较两个路径下DLL版本并出力CSV

本文介绍了一个实用的文件比较工具,能够读取指定路径信息,提取压缩文件,并比较两个目录下DLL文件的时间戳和大小,最终将比较结果输出为CSV文件。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

'''
Created on 2015年8月26日


@author: Administrator
'''
import os
import sys
import zipfile
import fnmatch
import datetime
import time
import csv


class Util(object):


   #read information from the txt file
   def readPathInfo(self,path):
       list=[]
       pathinfo=os.path.normpath(path)
       file_handle=open(pathinfo,"r")
       try:
           for path in file_handle:
               list.append(path.strip())
       except IOError:
           print("READ ERROR")
           sys.exit()
       finally:
           file_handle.close()
       return list
   
   #extract the zipmodule in the extraction_dir
   def zipExtractAll(self,zipmodule,extraction_dir):
       zipfiles=zipfile.ZipFile(zipmodule,"r")
       zipfiles.extractall(extraction_dir)
   
   #gain the modify time of the file
   #the size of the file
   def fileModifyTimeSize(self,filepath):
       list=[]
       stat=os.stat(filepath)
       last_modified = stat.st_mtime
       timeArray=time.localtime(last_modified)
       otherStyleTime = time.strftime("%Y-%m-%d %H:%M:%S", timeArray)
       
       size=os.path.getsize(filepath)
       list.append(otherStyleTime)
       list.append(size)
       return list
       
   #fnmatch *.zip
   #fnmatch *.dll
   def iterFindFiles(self,path, extension):
        for root, dirs, files in os.walk(path):
            for filename in fnmatch.filter(files, extension):
                yield os.path.join(root, filename)
   
   #gain the modules and properties
   def acquireModulesInfo(self,path,extension):
       dictInfo={}
       for filename in Util.iterFindFiles(self,path,extension):
           list=Util.fileModifyTimeSize(self,filename)
           dictInfo[filename]=list
       return dictInfo
  
   #compare the timestamp and output the csv file
   def compTimeSizeCsv(self,dictoriginal,dictcompare,csvfile):
       ##open the csv
       csvoutput=open(csvfile, 'w')
       fieldnames=['originalmodule','originaltimestamp','originalsize',
                       'comparemodule','comparetimestamp','comparesize','result']
       writer=csv.DictWriter(csvoutput, fieldnames=fieldnames)
       writer.writeheader()
       
       #prepare data for compare
       dictbasedir={}
       for key in dictcompare.keys():
           basename=os.path.basename(key)
           dirname=os.path.dirname(key)
           dictbasedir[basename]=dirname
       
       for count in range(0,len(dictoriginal.keys())):
           keyvalue=dictoriginal.popitem()
           
           if os.path.basename(keyvalue[0]) in dictbasedir.keys():
               keycompare=os.path.join(dictbasedir[os.path.basename(keyvalue[0])],os.path.basename(keyvalue[0]))
               listcompare=dictcompare.pop(keycompare)
               listoriginal=keyvalue[1]
               if listoriginal[0]==listcompare[0] and listoriginal[1]==listcompare[1]:
                   writer.writerow({'originalmodule':keyvalue[0],'originaltimestamp':listoriginal[0],'originalsize':listoriginal[1],
                       'comparemodule':keycompare,'comparetimestamp':listcompare[0],'comparesize':listcompare[1],'result':"OK"})
               else:
                   writer.writerow({'originalmodule':keyvalue[0],'originaltimestamp':listoriginal[0],'originalsize':listoriginal[1],
                       'comparemodule':keycompare,'comparetimestamp':listcompare[0],'comparesize':listcompare[1],'result':"NG"})  
       #dictoriginal
       for key in dictoriginal.keys():
           writer.writerow({'originalmodule':key,'originaltimestamp':dictoriginal[key][0],'originalsize':dictoriginal[key][1],
                       'comparemodule':"",'comparetimestamp':"",'comparesize':"",'result':""})
       #dictcompare
       for key in dictcompare.keys():
           writer.writerow({'originalmodule':"",'originaltimestamp':"",'originalsize':"",
                       'comparemodule':key,'comparetimestamp':dictcompare[key][0],'comparesize':dictcompare[key][1],'result':""})
       return True


#Test correctness of the code
if __name__=="__main__":
    util=Util()
    listpath=util.readPathInfo("C:\\path.txt")
    dictlist=[]
    for path in listpath:
        if os.path.isdir(path):         
            for filename in util.iterFindFiles(path, "*.zip"):
                util.zipExtractAll(filename,path)
            
            dictorigcomp=util.acquireModulesInfo(path,"*.dll")
            dictlist.append(dictorigcomp)
        else:
            print("the input information is not path")
    try:
        util.compTimeSizeCsv(dictlist[0], dictlist[1], "C:\\csvreport.csv")
        print("Successful")
    except Exception:
        print("Unsuccessful")
        
        
        
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值