您的位置:首页 > 编程语言 > Python开发

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()
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: