Python compresses data files generated on a weekly basis

Source: Internet
Author: User
Tags glob timedelta

This script was written to facilitate the collation of part of the business data, as well as storage management. Later, if there is demand, change it.
1 #!/usr/bin/env python2 #Coding:utf83 4 ImportOs,sys,time,commands,shutil,glob5 Importdatetime6 7 8 classFilesuo:9     Ten     def __init__(self,dir): OneSelf.dir =dir A      -     defgetfile (self): -          forRoot,dirs,filesinchOs.walk (self.dir): the             PrintDirs'\ n', files,'\ n' -     defGetsunday (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, +         #Get Date Parameters Aself.y = Int (Self.tmpstr.split (',') [0]) atself.m = Int (Self.tmpstr.split (',') [1]) -SELF.D = Int (Self.tmpstr.split (',') [2]) -         returnSelf.y,self.m,self.d,self.lastsunday -     defGetweek (self): -Week = {} -Self.iosformat ="%y%m%d" inSelf. Sunday = self. Getsunday () [3].strftime (Self.iosformat) -week['Sun'] =Self . Sunday toweek['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) theweek['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)Panax Notoginseng  -         returnWeek the  +  A  the 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' the     ifos.path.exists (dir): -         Print "dir is exits:%s"%dirWuyi         if  notos.path.exists (lastweekdir): the Os.mkdir (Lastweekdir) -              Wu         Else: -             Print "Lastweekdir is exits:%s"%Lastweekdir About          forK,mfinchF.getweek (). Items (): $ Os.chdir (dir) -             #print os.popen (' ls '). ReadLines () -Mvfilelist = Glob.glob (r"requestrecord.%s_*"%MF) -             #print Type (mvfilelist) A              forFileinchmvfilelist: +                 #Print file, the Shutil.move (File,lastweekdir) -  $     #count Lastweekdir Xia file number the         defcountfile (): theCount =0 the         returnLen (sum ([i[2] forIinchOs.walk (Lastweekdir)],[])) the              -     PrintCountfile () in     ifOs.path.exists (Lastweekdir) andCountfile ()! =0: theFlog = open (Maillog,'W') theFlog.write ("Yasuo%s Start date:%s"% (Lastweekdir,time.strftime ("%y-%m-%d%h:%m:%s", Time.localtime () ))) AboutFlog.write ("This file Yasuo number is%d"%countfile ()) the Os.chdir (dir) theStatus,result = Commands.getstatusoutput ('tar zcvf requestrecord%s.tar.gz requestrecord%s'%(Lastsun,lastsun)) the         #Print Status,result +         ifStatus = =0: -Flog.write ("Yasuo%s file is success, End Date:%s"% (Lastweekdir,time.strftime ("%y-%m-%d%h:%m:%s", Time.localtime () ))) theCommands.getstatusoutput ('RM-RF requestrecord%s'%Lastsun)Bayi         Else: theFlog.write ("Yasuo%s file is Faild .... .....") the  - flog.close () -  the          the          the          the     #print f.getfile () -     #Print Lastsun the     #print F.getweek ()

Python compresses data files generated on a weekly basis

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.