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

删除超过1年数据完整python脚本

时间:2018-06-12 12:17:14      阅读:168      评论:0      收藏:0      [点我收藏+]

标签:shutil

删除超过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" zhi.yang@ming.com < /log/110Hudrm/gmss2.txt‘)
:wq

python aa.py

删除超过1年数据完整python脚本

标签:shutil

原文地址:http://blog.51cto.com/yangzhiming/2128364

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