#!/usr/bin/env python # -*- coding: utf-8 -*- ''' Created on 2015年4月16日 @author : Administrator ''' ''' import os,time print time.ctime(os.path.getmtime(r"C:\aa\bb.txt")) 此脚本的作用: 1:比较文件是否发生变化,把发生变化的目录抓出来,放到临时备份目录 2:把临时存放的目录压缩 3:删除临时存放目录里边的文件 ''' import os,datetime import shutil import os,os.path import zipfile import time from os.path import join,getsize start_time=time.strftime('%Y-%m-%d') #获取系统的时间 copyFileCounts = 0 start_time_a=start_time+' '+'17:00:00' start_time_b=start_time+' '+'18:00:00' ''' 比较文件是否发生update,改变的文件抓出来 说明: 1:base_dir源目录 2:remote_dir临时存放目录 ''' def compare(base_dir,remote_dir): list = os.listdir(base_dir) filelist = [] for i in range(0, len(list)): path = os.path.join(base_dir,list[i]) if os.path.isfile(path): filelist.append(list[i]) for i in range(0, len(filelist)): path = os.path.join(base_dir, filelist[i]) if os.path.isdir(path): continue timestamp = os.path.getmtime(path) print timestamp ts1 = os.stat(path).st_mtime print ts1 date = datetime.datetime.fromtimestamp(timestamp) print list[i],' 最近修改时间是: ',date.strftime('%Y-%m-%d %H:%M:%S') #if time.strftime('%Y-%m-%d'+' '+'09:00:00')<date.strftime('%H:%M:%S')<time.strftime('%Y-%m-%d')+'12:00:00': #time.strftime('%Y-%m-%d %X',time.localtime(time.time())) aa=start_time+' '+'09:00:00' print aa ,'aa',date.strftime('%H:%M:%S') sufix = os.path.splitext(list[i])[1][1:] print sufix if (sufix=='per' and start_time_a<date.strftime('%Y-%m-%d %H:%M:%S')<start_time_b): #copyFiles(r'F:\bb',remote_dir) shutil.copy(os.path.join(base_dir, list[i]), os.path.join(remote_dir, list[i])) #isempty(r'F:\cc\kk') #zip_dir(remote_dir, r'f:\cc\zip.zip') #file_delete(remote_dir) ''' 把发生变化的文件抓出来进行压缩 说明: 1:dirname临时存放目录 2:zipfilename压缩目录 ''' def zip_dir(dirname,zipfilename): filelist = [] if os.path.isfile(dirname): filelist.append(dirname) else : for root, dirs, files in os.walk(dirname): for name in files: filelist.append(os.path.join(root, name)) zf = zipfile.ZipFile(zipfilename, "w", zipfile.zlib.DEFLATED) for tar in filelist: arcname = tar[len(dirname):] #print arcname zf.write(tar,arcname) zf.close() ''' 删除备份目录存放的文件 说明: 1:要删除的目录 ''' def file_delete(delDir): delList = [] delList = os.listdir(delDir) for f in delList: filePath = os.path.join(delDir,f) if os.path.isfile(filePath): sufix = os.path.splitext(f)[1][1:] print sufix if(sufix=='per'): os.remove(filePath) print filePath + " was removed!" elif os.path.isdir(filePath): shutil.rmtree(filePath,True) print "Directory: " + filePath +" was removed!" ''' 程序的入口 ''' def isempty(folderpath): #定义文件夹路径 #设置文件夹内容是否为空标识符 empty=False #os.path.exists(folderpath.encode('GBK'))路径如果存在,返回True if(os.path.exists(folderpath.encode('GBK'))==False): #文件夹不存在时 #doing something... print '该文件不存在' else: for root,dirs, files in os.walk(folderpath.encode('GBK')): if(len(files)==0): empty=True if(empty): #文件夹内容为空 #doing something... shutil.rmtree(folderpath) print '已经删除!' def copyFiles(sourceDir, targetDir): global copyFileCounts print sourceDir print u"%s 当前处理文件夹%s已处理%s 个文件" %(time.strftime('%Y-%m-%d %H:%M:%S',time.localtime(time.time())), sourceDir,copyFileCounts) for f in os.listdir(sourceDir): sourceF = os.path.join(sourceDir, f) targetF = os.path.join(targetDir, f) if os.path.isfile(sourceF): #创建目录 if not os.path.exists(targetDir): os.makedirs(targetDir) copyFileCounts += 1 #文件不存在,或者存在但是大小不同,覆盖 if not os.path.exists(targetF) or (os.path.exists(targetF) and (os.path.getsize(targetF) != os.path.getsize(sourceF))): #2进制文件 open(targetF, "wb").write(open(sourceF, "rb").read()) print u"%s %s 复制完毕" %(time.strftime('%Y-%m-%d %H:%M:%S',time.localtime(time.time())), targetF) else: print u"%s %s 已存在,不重复复制" %(time.strftime('%Y-%m-%d %H:%M:%S',time.localtime(time.time())), targetF) if os.path.isdir(sourceF): copyFiles(sourceF, targetF) def getdirsize(dir): size=0l for (root,dirs,files) in os.walk(dir): for name in files: try: #print getsize(join(root,name)) size += getsize(join(root,name)) except: continue #直接用下面这句代码,在ubuntu 会出错,在windows 下没测试过。 #size += sum([getsize(join(root,name)) for name in files]) return size if __name__ == '__main__': ''' 1 ''' compare(r'D:\METROLOG\donnees\bdmt\fimesure\XZH_fimesure\afflx',r'D:\CFT_DATA\LIVE\LOCAL\Metrolog\temple\XZH_fimesure\afflx') ''' 2 ''' compare(r'D:\METROLOG\donnees\bdmt\fimesure\XZH_fimesure\caisse',r'D:\CFT_DATA\LIVE\LOCAL\Metrolog\temple\XZH_fimesure\caisse') ''' 3 ''' compare(r'D:\METROLOG\donnees\bdmt\fimesure\XZH_fimesure\emboutissage',r'D:\CFT_DATA\LIVE\LOCAL\Metrolog\temple\XZH_fimesure\emboutissage') ''' 4 ''' compare(r'D:\METROLOG\donnees\bdmt\fimesure\XZH_fimesure\liaisonsol',r'D:\CFT_DATA\LIVE\LOCAL\Metrolog\temple\XZH_fimesure\liaisonsol') ''' 5 ''' compare(r'D:\METROLOG\donnees\bdmt\fimesure\XZH_fimesure\ouvrants',r'D:\CFT_DATA\LIVE\LOCAL\Metrolog\temple\XZH_fimesure\ouvrants') ''' 6 ''' compare(r'D:\METROLOG\donnees\bdmt\fimesure\XZH_fimesure\units',r'D:\CFT_DATA\LIVE\LOCAL\Metrolog\temple\XZH_fimesure\units') ''' 7 ''' compare(r'D:\METROLOG\donnees\bdmt\fimesure\XZJ_fimesure\caisse',r'D:\CFT_DATA\LIVE\LOCAL\Metrolog\temple\XZJ_fimesure\caisse') ''' 8 ''' compare(r'D:\METROLOG\donnees\bdmt\fimesure\XZJ_fimesure\emboutissage',r'D:\CFT_DATA\LIVE\LOCAL\Metrolog\temple\XZJ_fimesure\emboutissage') ''' 9 ''' compare(r'D:\METROLOG\donnees\bdmt\fimesure\XZJ_fimesure\liaisonsol',r'D:\CFT_DATA\LIVE\LOCAL\Metrolog\temple\XZJ_fimesure\liaisonsol') ''' 10 ''' compare(r'D:\METROLOG\donnees\bdmt\fimesure\XZJ_fimesure\ouvrants',r'D:\CFT_DATA\LIVE\LOCAL\Metrolog\temple\XZJ_fimesure\ouvrants') ''' 11 ''' compare(r'D:\METROLOG\donnees\bdmt\fimesure\XZJ_fimesure\units',r'D:\CFT_DATA\LIVE\LOCAL\Metrolog\temple\XZJ_fimesure\units') filesize1 = getdirsize(r'D:\CFT_DATA\LIVE\LOCAL\Metrolog\temple\XZH_fimesure') filesize2 = getdirsize(r'D:\CFT_DATA\LIVE\LOCAL\Metrolog\temple\XZJ_fimesure') if filesize1>0: zip_dir(r'D:\CFT_DATA\LIVE\LOCAL\Metrolog\temple\XZH_fimesure',r'F:\nn\haha.zip') if filesize2>0: zip_dir(r'D:\CFT_DATA\LIVE\LOCAL\Metrolog\temple\XZJ_fimesure',r'F:\nn\hehe.zip') ''' 1 ''' file_delete(r'D:\CFT_DATA\LIVE\LOCAL\Metrolog\temple\XZH_fimesure\afflx') ''' 2 ''' file_delete(r'D:\CFT_DATA\LIVE\LOCAL\Metrolog\temple\XZH_fimesure\caisse') ''' 3 ''' file_delete(r'D:\CFT_DATA\LIVE\LOCAL\Metrolog\temple\XZH_fimesure\emboutissage') ''' 4 ''' file_delete(r'D:\CFT_DATA\LIVE\LOCAL\Metrolog\temple\XZH_fimesure\liaisonsol') ''' 5 ''' file_delete(r'D:\CFT_DATA\LIVE\LOCAL\Metrolog\temple\XZH_fimesure\ouvrants') ''' 6 ''' file_delete(r'D:\CFT_DATA\LIVE\LOCAL\Metrolog\temple\XZH_fimesure\units') ''' 7 ''' file_delete(r'D:\CFT_DATA\LIVE\LOCAL\Metrolog\temple\XZJ_fimesure\caisse') ''' 8 ''' file_delete(r'D:\CFT_DATA\LIVE\LOCAL\Metrolog\temple\XZJ_fimesure\emboutissage') ''' 9 ''' file_delete(r'D:\CFT_DATA\LIVE\LOCAL\Metrolog\temple\XZJ_fimesure\liaisonsol') ''' 10 ''' file_delete(r'D:\CFT_DATA\LIVE\LOCAL\Metrolog\temple\XZJ_fimesure\ouvrants') ''' 11 ''' file_delete(r'D:\CFT_DATA\LIVE\LOCAL\Metrolog\temple\XZJ_fimesure\units')