删除超过1年数据完整python脚本:
vi aa.py
#!/usr/bin/python
import os
import datetime
import shutil
dir="/aa"
now = datetime.datetime.now()
delta = datetime.timedelta(days=365)
os.remove("/log/110Hudrm/gmss.txt")
os.mknod("/log/110Hudrm/gmss.txt")
os.chdir(dir)
str1 = "_W"
for root,dirs,files in os.walk(dir):
for f in dirs:
if f == "Ima":
mtime = datetime.datetime.fromtimestamp(os.path.getmtime(os.path.join(root,f)))
if mtime < (now - delta):
b = os.path.dirname(os.path.join(root,f))
if str1 in b:
d = os.path.dirname(os.path.join(root,f))
p = open(‘/log/110Hudrm/gmss.txt‘,‘a‘)
p.write(d+"\n")
p.close()
size = 0
c = open(‘/log/110Hudrm/gmss.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 = size / 1024 / 1024 / 1024
e = open(‘/log/110Hudrm/gmss2.txt‘,‘w‘)
e.write(str(si)+‘G‘)
e.close()
c = open(‘/log/110Hudrm/gmss.txt‘,‘r‘)
for i in c.readlines():
shutil.rmtree(i.strip())
os.system(‘/usr/bin/mail -s "host1(10.0.0.2) aa delete(over 1 years) Size at date +%Y-%m-%d
" [email protected] < /log/110Hudrm/gmss2.txt‘)
:wq
python aa.py
原文地址:http://blog.51cto.com/yangzhiming/2128364