首页 / PYTHON / Python Tool Kits
Python Tool Kits
内容导读
互联网集市收集整理的这篇技术教程文章主要介绍了Python Tool Kits,小编现在分享给大家,供广大互联网技能从业者学习和参考。文章包含3296字,纯文字阅读大概需要5分钟。
内容图文
![Python Tool Kits](/upload/InfoBanner/zyjiaocheng/856/bf5ff016c1b84cfcafa1dced134920d0.jpg)
#检查软件更新版本与旧版本之间的文件差异
#check two folder list the difference: input folderpath, output modification
#output:{'New':[],'Delete':[],'Modification':[]}
#!有同名文件
def ParsedPathList(PathList,WorkPath):
NameList=[]
for path in PathList:
NameList.append(path.split(WorkPath)[-1])
return NameList
def Nor2List(ListPathA,ListPathB,PathA,PathB):
NorList=[]
ComList=[]
ListA=ParsedPathList(ListPathA,PathA)
ListB=ParsedPathList(ListPathB,PathB)
ia=0
while(ia<len(ListA)):
ib=0
Lstart=len(ComList)
while(ib<len(ListB)):
if(ListA[ia]==ListB[ib]):
ComList.append(ListPathA[ia])
ib=ib+1
Lend=len(ComList)
if Lstart==Lend:
NorList.append(ListPathA[ia])
ia=ia+1
return NorList,ComList
#该函数返回PATH目录下所有的文件List
import os
def recursionFile(PATH):
if(PATH[-1]!='/'):
PWD=PATH+'/'
else:
PWD=PATH # 如果路径已经有有反斜杠就不用加了
Files=[]
#列出当前目录下文件和目录
CurrentDir=os.listdir(PATH)
#如果都是文件,则结束,否则分成folder和file,示性向量-1,1
for i in range (len(CurrentDir)):
#如果CurrentDir下的元素不是目录则查看文件类型,收集文件,若是目录则递归
if(os.path.isdir(PWD+CurrentDir[i])):
Files=Files+(recursionFile(PWD+CurrentDir[i]))
elif(os.path.isfile(PWD+CurrentDir[i])):
Files.append(PWD+CurrentDir[i])
return Files
import time
import hashlib
def CountExcutionTime(DifferenceByMD5):
def wrapper(*args, **kwargs):
startime=time.time()
res=DifferenceByMD5(*args, **kwargs)
#有返回数值函数使用装饰器,要将返回值一层层传出来
terminaltime=time.time()
runtime = terminaltime-startime
print("time is %d ms" %runtime)
return res
return wrapper
@CountExcutionTime
def DifferenceByMD5(NewFolder,OldFolder):
NewFolderList=recursionFile(NewFolder)
OldFolderList=recursionFile(OldFolder)
#Search string from new in old folder
NewNorList,ComListNew=Nor2List(NewFolderList,OldFolderList,NewFolder,OldFolder)
OldNorList,ComListOld=Nor2List(OldFolderList,NewFolderList,OldFolder,NewFolder)
Modification=[]
Equal=[]
NewNameList=ParsedPathList(ComListNew,NewFolder)
OldNameList=ParsedPathList(ComListOld,OldFolder)
newi=0
while(newi<len(NewNameList)):
NewFileName=NewNameList[newi]
oldi=0
while(oldi<len(OldNameList)):
OldFileName=OldNameList[oldi]
if (NewFileName==OldFileName):
NewFilePath=ComListNew[newi]
OldFilePath=ComListOld[oldi]
NewMD5=hashlib.md5(open(NewFilePath,'rb').read()).hexdigest()
OldMD5=hashlib.md5(open(OldFilePath,'rb').read()).hexdigest()
if NewMD5!=OldMD5:
Modification.append(NewFilePath)
else:
Equal.append(NewFilePath)
oldi+=1
newi+=1
return {'Modify':Modification,'New Create':NewNorList,'Delete Old':OldNorList,'Equal':Equal}
#Code example:
WorkPath='D:/Script/PDKVersion/1'
NewFolder=WorkPath+'/'+'software_v1.0.1'
OldFolder=WorkPath+'/'+'software_v1.0.0'
res=DifferenceByMD5(NewFolder,OldFolder)
内容总结
以上是互联网集市为您收集整理的Python Tool Kits全部内容,希望文章能够帮你解决Python Tool Kits所遇到的程序开发问题。 如果觉得互联网集市技术教程内容还不错,欢迎将互联网集市网站推荐给程序员好友。
内容备注
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 gblab@vip.qq.com 举报,一经查实,本站将立刻删除。
内容手机端
扫描二维码推送至手机访问。