鸿 网 互 联 www.68idc.cn

当前位置 : 服务器租用 > 编程语言开发 > python > >

贴一贴自己写的文件监控代码python

来源:互联网 作者:佚名 时间:2013-05-18 11:52
Created on 2013-1-29 3 4 @author: zsy os.path 7 import time 8 import smtplib 9 from email.mime.text import MIMEText 10 from email.mime.multipart import MIMEMultipart 11 from email.mime.audio import MIMEAudio Scanner(): 14 def isExists(self

Created on 2013-1-29 3 4 @author: zsy os.path 7 import time 8 import smtplib 9 from email.mime.text import MIMEText 10 from email.mime.multipart import MIMEMultipart 11 from email.mime.audio import MIMEAudio Scanner(): 14 def isExists(self,saveFile): 15 return os.path.isfile(saveFile) getFileName(self,targetDir,whiteDir): 18 export = [] 19 export_notime=[] 20 for root, dirs, fileNames in os.walk(targetDir): 21 for i,dirname in enumerate(dirs): 22 if dirname in whiteDir:del dirs[i] 23 if fileNames: 24 for filename in fileNames: 25 filepath=os.path.join(root,filename) 26 if os.path.islink(filepath): : ,time.localtime(os.stat(filepath).st_mtime)) +filetime+) 31 export_notime.append(filepath) 32 except: ) 34 export_notime.append(filepath) 35 return export,export_notime scan(self,targetDir,whiteDir, saveFile): ) 39 scanlist,scanlist_notime=self.getFileName(targetDir,whiteDir) 40 for ifile in scanlist: 41 try: 42 file.write(ifile) 43 except: ).decode()) 45 file.close() 46 return scanlist,scanlist_notime rescan(self,targetDir,whiteDir,saveFile,changFile): filelist = []filelist_notime=[] ) 53 line = read.readline() 54 while line: )[0] 56 filelist.append(line) 57 filelist_notime.append(linenotime) 58 line = read.readline() 59 read.close() scanlist,scanlist_notime = self.scan(targetDir,whiteDir,saveFile)fileset = set(filelist) 65 fileset_notime=set(filelist_notime) 66 scanset = set(scanlist) 67 scanset_notime=set(scanlist_notime) 68 69 deletelist=[i for i in fileset_notime-scanset_notime] 70 71 addlist_notime=[i for i in scanset_notime-fileset_notime] 72 addlist=[] 73 for item in addlist_notime: , time.localtime(os.stat(item).st_mtime)) +mtime+) 76 77 modifylist=[i for i in scanset-fileset] 78 modifylist=[i for i in set(modifylist)-set(addlist)] deletelist.__len__() > 0: ) +time.strftime(,time.localtime(time.time()))+ fchange.write(title) 87 sendstr+=title 88 i=0 89 for deletefile in deletelist: deletefile: 92 try: +deletefile.encode().decode()+): +deletefile+)sendstr+=str(i)++deletefile+ deletefile: 99 try: +deletefile.encode().decode()+): +deletefile+)sendstr+=str(i)++deletefile+: 106 try: ).decode()+): )sendstr+=str(i)+i+=1 113 fchange.close() addlist.__len__() > 0: ) +time.strftime(,time.localtime(time.time()))+ fchange.write(title) 119 sendstr+=title 120 i=0 121 for addfile in addlist: addfile: 124 try: +addfile.encode().decode()): +addfile)sendstr+=str(i)++addfile addfile: 131 try: +addfile.encode().decode()): +addfile)sendstr+=str(i)++addfile : 138 try: ).decode()): sendstr+=str(i)+" "+addfile 143 144 i+=1 145 fchange.close() modifylist.__len__()>0: ) +time.strftime(,time.localtime(time.time()))+ fchange.write(title) 151 sendstr+=title 152 i=0 153 for modifyfile in modifylist: modifyfile: 156 try: +modifyfile.encode().decode()): +modifyfile)sendstr+=str(i)++modifyfile modifyfile: 163 try: +modifyfile.encode().decode()): +modifyfile)sendstr+=str(i)++modifyfile : 170 try: ).decode()): sendstr+=str(i)+" "+modifyfile 175 176 i+=1 177 fchange.close() sendstr.__len__()>0: sendMail(self,body): 184 m=MIMEMultipart() ]=m[]=m.attach(MIMEText(body,,))smtpserver=smtplib.SMTP() ) toadds=[] 193 for toadd in toadds: ]=toadd 195 smtpserver.sendmail(fromadd, toadd, m.as_string()) 196 smtpserver.close() == : whiteDir=[,] changeFile=scanner=Scanner() 210 if scanner.isExists(myFile): 211 scanner.rescan(Dir,whiteDir,myFile,changeFile) 212 else: 213 scanner.scan(Dir,whiteDir,myFile) ,香港服务器租用美国服务器香港空间

网友评论
<