python 压缩每周生成的数据文件
2015-06-24 23:22
579 查看
为了便于整理部分业务数据,以及存储管理, 写了此脚本.后期如果有需求,再改一下.
#!/usr/bin/env python #coding:utf8 import os,sys,time,commands,shutil,glob import datetime class FileSuo: def __init__(self,dir): self.dir = dir def getfile(self): for root,dirs,files in os.walk(self.dir): print dirs,'\n',files,'\n' def GetSunday(self): #isoformat = "%Y%m%d" self.lastsunday = datetime.date.today() - datetime.timedelta(days=datetime.date.today().isoweekday()) self.tmpstr = self.lastsunday.strftime("%Y,%m,%d") #print self.tmpstr, #得到日期参数 self.y = int(self.tmpstr.split(',')[0]) self.m = int(self.tmpstr.split(',')[1]) self.d = int(self.tmpstr.split(',')[2]) return self.y,self.m,self.d,self.lastsunday def Getweek(self): week = {} self.iosformat = "%Y%m%d" self.Sunday = self.GetSunday()[3].strftime(self.iosformat) week['Sun'] = self.Sunday week['Sat'] = (datetime.date(self.GetSunday()[0],self.GetSunday()[1],self.GetSunday()[2])+datetime.timedelta(days=-1)).strftime(self.iosformat) week['Fri'] = (datetime.date(self.GetSunday()[0],self.GetSunday()[1],self.GetSunday()[2])+datetime.timedelta(days=-2)).strftime(self.iosformat) week['Thur'] = (datetime.date(self.GetSunday()[0],self.GetSunday()[1],self.GetSunday()[2])+datetime.timedelta(days=-3)).strftime(self.iosformat) week['Wed'] = (datetime.date(self.GetSunday()[0],self.GetSunday()[1],self.GetSunday()[2])+datetime.timedelta(days=-4)).strftime(self.iosformat) week['Tue'] = (datetime.date(self.GetSunday()[0],self.GetSunday()[1],self.GetSunday()[2])+datetime.timedelta(days=-5)).strftime(self.iosformat) week['Mon'] = (datetime.date(self.GetSunday()[0],self.GetSunday()[1],self.GetSunday()[2])+datetime.timedelta(days=-6)).strftime(self.iosformat) return week if __name__ == "__main__": dir = r'/share/WebService_Device_UserMsg/record/file' f = FileSuo(dir) lastsun = f.GetSunday()[3].strftime("%Y%m%d") lastweekdir = dir + '/' + 'requestRecord%s'%lastsun maillog = dir + '/' + 'requestRecordmail.log' if os.path.exists(dir): print "dir is exits: %s" %dir if not os.path.exists(lastweekdir): os.mkdir(lastweekdir) else: print "lastweekdir is exits: %s" %lastweekdir for k,mf in f.Getweek().items(): os.chdir(dir) #print os.popen('ls').readlines() mvfilelist = glob.glob(r"requestRecord.%s_*"%mf) #print type(mvfilelist) for file in mvfilelist: #print file, shutil.move(file,lastweekdir) #count lastweekdir xia file number def countfile(): count = 0 return len(sum([i[2] for i in os.walk(lastweekdir)],[])) print countfile() if os.path.exists(lastweekdir) and countfile() != 0: flog = open(maillog,'w') flog.write(" yasuo %s start date: %s" %(lastweekdir,time.strftime("%Y-%m-%d %H:%M:%S", time.localtime()))) flog.write("this file yasuo number is %d" %countfile()) os.chdir(dir) status,result = commands.getstatusoutput('tar zcvf requestRecord%s.tar.gz requestRecord%s' %(lastsun,lastsun)) #print status,result if status == 0: flog.write("yasuo %s file is success ,end date: %s"%(lastweekdir,time.strftime("%Y-%m-%d %H:%M:%S", time.localtime()))) commands.getstatusoutput('rm -rf requestRecord%s' %lastsun) else: flog.write("yasuo %s file is faild .............") flog.close() #print f.getfile() #print lastsun #print f.Getweek()
相关文章推荐
- python常用函数库及模块巧妙用法汇总
- python Multiprocessing 多进程应用
- Python输出字符串或文件颜色显示
- 免密码登录服务器python脚本
- python 常用模块之ConfigParser
- Python偏函数
- Python完善decorator
- python版本的curl工具pycurl学习
- Python学习日记---文件操作
- Python学习笔记
- Show SN: 北京实时公交Python接口 (github.com)
- pImpl用法在Python的示例代码
- Python——处理数字计算
- 序列判断
- 【Python】动态获取python类名、函数名&多线程
- python 模块导入
- python 中面向对象编程简单总结3--定制类
- python学习随笔
- python __file__的绝对路径与import的搜索路径
- python学习笔记二