-
Notifications
You must be signed in to change notification settings - Fork 0
/
logzip.py
47 lines (35 loc) · 1007 Bytes
/
logzip.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
#!/usr/bin/python
# -*-coding=utf_8 -*-
import os
import tarfile
import datetime
def tarer(fname, files):
t = tarfile.open(fname + ".tar.gz", "w:gz")
for file in files:
t.add(file)
t.close()
def zip_dir(dirname, patterns, fname):
filelist = []
if os.path.isfile(dirname):
filelist.append(dirname)
else:
for root, dirs, files in os.walk(dirname):
for name in files:
for p in patterns:
if p in name:
filelist.append(os.path.join(root, name))
tarer(fname, filelist)
for file in filelist:
os.remove(file)
def day_get(d, num):
oneday = datetime.timedelta(days=num)
day = d - oneday
return day.strftime('%Y-%m-%d')
def logzip(num):
d = datetime.datetime.now()
days = []
for i in range(1, num):
days.append(day_get(d, i))
zip_dir('./', days, '%s__%ddays' % (d.strftime('%Y-%m-%d'), num))
if __name__ == '__main__':
logzip(10)