1#coding=utf8 2''' 3该库用来管理文件。 4初始化函数调用读取配置文件模块中的Config类 5用来获取下载路径、保存路径。 6模块包含四个方法: 7clearResultCSV(): 用来删除下载路径下所有的result开头的csv文件 8moveCSVToSave():把下载路径下的result.csv文件重命名,并把重命名后的文件移动到保存路径下 9getLastFileWithPath():获取保存路径下最新的文件,并带路径返回该文件 10getLastFile():获得最新文件的命令并返回 11 12''' 13import os 14#操作文件的包 15import shutil 16import re 17import time 18import difflib 19#导入读取配置文件库的Config 20from readConfig import Config 21 22class FileManger(object): 23 def __init__(self,configObj=Config()): 24 try: 25 #创建一个Config对象实例 26 self.config=configObj 27 #通过对象实例调用方法getDownPath() 28 #获取下载路径 29 self.down=self.config.getDownPath() 30 #通过对象实例调用方法getSavePath() 31 #获取保存路径 32 try: 33 self.save=self.config.getSavePath() 34 if os.path.exists(self.save): 35 pass 36 else: 37 os.mkdir(self.save) 38 self.save=self.save 39 except Exception,e: 40 print "Save Report Error:",e 41 except Exception,e: 42 print e 43 44 45 def clearResultCSV(self): 46 try: 47 #获取下载路径下的所有文件 48 #并把文件保存在list变量fileList中 49 fileList=os.listdir(self.down) 50 #判断fileList是否为空,不为空执行if模块 51 if fileList: 52 #对fileList中的元素进行循环 53 for item in fileList: 54 #查找下载路径下是否存在result开头的csv文件 55 #如果存在,则删除 56 if re.match("result(.*).csv",item): 57 #删除result开头的csv文件 58 os.remove(self.down+"\\"+item) 59 except Exception,e: 60 print e 61 62 def moveCSVToSave(self): 63 try: 64 #获取下载路径下的所有文件 65 #并把文件保存在list变量fileList中 66 fileList=os.listdir(self.down) 67 #获取当前时间并转换为字符串格式 68 now=time.strftime("%Y%m%d%H%M%S") 69 #判断fileList是否为空,不为空执行if模块 70 if fileList: 71 #对fileList中的元素进行循环 72 for item in fileList: 73 try: 74 #查找下载路径下是否存在result.csv文件 75 #如果存在,对文件进行重命名 76 if re.match("result\.csv",item): 77 #获取带有路径的result.csv文件 78 oldfilename=self.down+"\\"+item 79 #重命名的命令格式是符:20170306143330.csv 80 newfileName=self.down+"\\"+now+".csv" 81 #对文件result.csv进行重命名为格式如:20170306143330.csv 82 os.rename(oldfilename,newfileName) 83 #把重命名的文件移动到保存路径下 84 shutil.move(newfileName, self.save) 85 except Exception,e: 86 print e 87 except Exception,e: 88 print e 89 90 def getLastReqement(self): 91 try: 92 #获取下载路径下的所有文件 93 #并把文件保存在list变量listfile中 94 listfile=os.listdir(self.save) 95 #判断listfile是否为空,不为空执行if模块 96 if len(listfile)>1: 97 #保存带有路径的最新文件 98 try: 99 self.diffPath=self.config.getDiffPath() 100 if os.path.exists(self.diffPath): 101 pass 102 else: 103 os.mkdir(self.diffPath) 104 self.diffPath=self.diffPath 105 106 lastfile=self.save+"\\"+listfile[-1] 107 #获取第二个比较新的文档 108 twofile=self.save+"\\"+listfile[-2] 109 lastestFile=open(lastfile,"r").readlines() 110 secondLastestFile=open(twofile,"r").readlines() 111 diffInf=difflib.ndiff(lastestFile,secondLastestFile) 112 diffHtml=open(self.diffPath+"\\"+"diff.log","w") 113 diffHtml.writelines(diffInf) 114 except Exception,e: 115 print "Save Diff Error:",e 116 except Exception,e: 117 print e 118 119 120def test(): 121 ''' 122 创建一个测试脚本,执行冒烟测试。 123 用来验证程序功能能正常运行。 124 ''' 125 #创建一个Config对象实例 126 fm=FileManger() 127 #fm.clearResultCSV() 128 129 fm.moveCSVToSave() 130 print fm.getLastFileWithPath(),os.listdir(fm.save) 131 132if __name__=="__main__": 133 test()
Python使用difflib对比两个文件操作实例
Wesley13
2021-10-11
1000 0 0
点赞
收藏
评论区
加载中...