标签:image walk day 通过 [] .com reverse == datetime
删除超过10天的数据:注:
1、or表示或者,相当于shell里的"||"(必须同时满足为and)
2、通过if not进行去重
3、key=len,reverse=True按路径长度从长到短排序(倒序)
#!/usr/bin/python
import os
import datetime
import shutil
dir="/gms2/Jenkins"
now = datetime.datetime.now()
delta = datetime.timedelta(days=10)
os.remove("/gms2/Scripts/log/rmgmss.txt")
os.mknod("/gms2/Scripts/log/rmgmss.txt")
os.remove("/gms2/Scripts/log/rmgms.txt")
os.mknod("/gms2/Scripts/log/rmgms.txt")
os.remove("/gms2/Scripts/log/rmgms2.txt")
os.mknod("/gms2/Scripts/log/rmgms2.txt")
os.chdir(dir)
for root,dirs,files in os.walk(dir):
for f in dirs:
if f == "Images" or f == "OTA_user_gms" or f == "OTA_user_native" of f == "IMG":
mtime = datetime.datetime.fromtimestamp(os.path.getmtime(os.path.join(root,f)))
if mtime < (now - delta):
z = open(‘/gms2/Scripts/log/rmgms2.txt‘,‘a‘)
z.write(str(mtime)+"\n")
z.close()
b = os.path.dirname(os.path.join(root,f))
p = open(‘/gms2/Scripts/log/rmgmss.txt‘,‘a‘)
p.write(b+"\n")
p.close()
list = []
bc = open(‘/gms2/Scripts/log/rmgmss.txt‘,‘r‘)
for bd in bc.readlines():
if not bd in list:
list.append(bd)
bc.close()
f2 = open(‘/gms2/Scripts/log/rmgms.txt‘,‘a‘)
for be in list:
f2.write(be)
f2.close()
size = 0
c = open(‘/gms2/Scripts/log/rmgms.txt‘,‘r‘)
for i in c.readlines():
for root,dirs,files in os.walk(i.strip()):
size += sum(os.path.getsize(os.path.join(root,fi)) for fi in files)
si = (format((float(size) / 1024 / 1024 / 1024),‘.1f‘))
e = open(‘/gms2/Scripts/log/rmgms3.txt‘,‘w‘)
e.write(str(si)+‘G‘)
e.close()
y = []
x = open(‘/gms2/Scripts/log/rmgms.txt‘,‘r‘)
for n in x.readlines():
y.append(n.strip())
for m in sorted(y,key=len,reverse=True):
shutil.rmtree(m)
os.system(‘/usr/bin/mail -s "host1(10.0.0.2) gms delete(over 10 days) Size at date +%Y-%m-%d
" zhi.yang@ming.com < /gms2/Scripts/log/rmgms3.txt‘)
:wq
标签:image walk day 通过 [] .com reverse == datetime
原文地址:http://blog.51cto.com/yangzhiming/2133011