码迷,mamicode.com
首页 > 编程语言 > 详细

python 压缩每周生成的数据文件

时间:2015-06-24 23:59:30      阅读:318      评论:0      收藏:0      [点我收藏+]

标签:

为了便于整理部分业务数据,以及存储管理, 写了此脚本.后期如果有需求,再改一下.
 1 #!/usr/bin/env python
 2 #coding:utf8
 3 
 4 import os,sys,time,commands,shutil,glob
 5 import datetime
 6 
 7 
 8 class FileSuo:
 9     
10     def __init__(self,dir):
11         self.dir = dir
12     
13     def getfile(self):
14         for root,dirs,files in os.walk(self.dir):
15             print dirs,\n,files,\n
16     def GetSunday(self):
17         #isoformat = "%Y%m%d"
18         self.lastsunday = datetime.date.today() - datetime.timedelta(days=datetime.date.today().isoweekday())
19         self.tmpstr = self.lastsunday.strftime("%Y,%m,%d")
20         #print self.tmpstr,
21         #得到日期参数
22         self.y = int(self.tmpstr.split(,)[0])
23         self.m = int(self.tmpstr.split(,)[1])
24         self.d = int(self.tmpstr.split(,)[2])
25         return self.y,self.m,self.d,self.lastsunday
26     def Getweek(self):
27         week = {}
28         self.iosformat = "%Y%m%d"
29         self.Sunday = self.GetSunday()[3].strftime(self.iosformat)
30         week[Sun] = self.Sunday
31         week[Sat] = (datetime.date(self.GetSunday()[0],self.GetSunday()[1],self.GetSunday()[2])+datetime.timedelta(days=-1)).strftime(self.iosformat)
32         week[Fri] = (datetime.date(self.GetSunday()[0],self.GetSunday()[1],self.GetSunday()[2])+datetime.timedelta(days=-2)).strftime(self.iosformat)
33         week[Thur] = (datetime.date(self.GetSunday()[0],self.GetSunday()[1],self.GetSunday()[2])+datetime.timedelta(days=-3)).strftime(self.iosformat)
34         week[Wed] = (datetime.date(self.GetSunday()[0],self.GetSunday()[1],self.GetSunday()[2])+datetime.timedelta(days=-4)).strftime(self.iosformat)
35         week[Tue] = (datetime.date(self.GetSunday()[0],self.GetSunday()[1],self.GetSunday()[2])+datetime.timedelta(days=-5)).strftime(self.iosformat)
36         week[Mon] = (datetime.date(self.GetSunday()[0],self.GetSunday()[1],self.GetSunday()[2])+datetime.timedelta(days=-6)).strftime(self.iosformat)
37 
38         return week
39 
40 
41 
42 if __name__ == "__main__":
43 
44     dir = r/share/WebService_Device_UserMsg/record/file
45     f = FileSuo(dir)
46     lastsun = f.GetSunday()[3].strftime("%Y%m%d")
47     lastweekdir = dir + / + requestRecord%s%lastsun
48     maillog = dir + / + requestRecordmail.log
49     if os.path.exists(dir):
50         print "dir is exits: %s" %dir
51         if not os.path.exists(lastweekdir):
52             os.mkdir(lastweekdir)
53             
54         else:
55             print "lastweekdir is exits: %s" %lastweekdir
56         for k,mf in f.Getweek().items():
57             os.chdir(dir)
58             #print os.popen(‘ls‘).readlines()
59             mvfilelist =  glob.glob(r"requestRecord.%s_*"%mf)
60             #print type(mvfilelist)
61             for file in mvfilelist:
62                 #print file,
63                 shutil.move(file,lastweekdir)
64 
65     #count lastweekdir xia file number  
66         def countfile():
67         count = 0
68         return  len(sum([i[2] for i in os.walk(lastweekdir)],[]))
69             
70     print countfile()
71     if os.path.exists(lastweekdir) and countfile() != 0:
72         flog = open(maillog,w)
73         flog.write(" yasuo %s start date: %s" %(lastweekdir,time.strftime("%Y-%m-%d %H:%M:%S", time.localtime())))
74         flog.write("this file yasuo number is %d" %countfile())
75         os.chdir(dir)   
76         status,result = commands.getstatusoutput(tar zcvf requestRecord%s.tar.gz requestRecord%s %(lastsun,lastsun))
77         #print status,result
78         if status == 0:
79             flog.write("yasuo %s file is success ,end date: %s"%(lastweekdir,time.strftime("%Y-%m-%d %H:%M:%S", time.localtime())))
80             commands.getstatusoutput(rm -rf requestRecord%s %lastsun)
81         else:
82             flog.write("yasuo %s file is faild .............")
83 
84         flog.close()
85 
86         
87         
88         
89     #print f.getfile()
90     #print lastsun
91     #print f.Getweek()

 

python 压缩每周生成的数据文件

标签:

原文地址:http://www.cnblogs.com/shantu/p/4598937.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!