码迷,mamicode.com
首页 > 其他好文 > 详细

找出1个小时前更新的文件并进行拷贝

时间:2018-06-20 21:35:54      阅读:156      评论:0      收藏:0      [点我收藏+]

标签:get   gms   strftime   tools   文件   目录   dirname   load   拷贝   

找出1个小时前更新的文件并进行拷贝:

注:
1、根据时间戳判断,非60分钟
2、os.makedirs递归创建目录路径

vi a1.py

#!/usr/bin/python
#coding=utf-8

import os
import datetime
from itertools import chain
import shutil
import sys
import time

reload(sys)
sys.setdefaultencoding(‘utf-8‘)

dir = "/ming1"
dst = "/ming2"

dirr = (‘aa‘,‘bb‘)

now = datetime.datetime.now()
d1 = (now - datetime.timedelta(hours=1)).strftime("%Y-%m-%d %H")

#os.remove("/log/110Hud/ingmss.txt")
#os.mknod("/log/110Hud/ingmss.txt")
#os.remove("/log/110Hud/ingmss2.txt")
#os.mknod("/log/110Hud/ingmss2.txt")
#os.remove("/log/110Hud/ingmss3.txt")
#os.mknod("/log/110Hud/ingmss3.txt")

str1 = "_W"

os.chdir(dir)

for root,dirs,files in chain.from_iterable(os.walk(root) for root in dirr):
for f in dirs:
if str1 in f:
mtime = (datetime.datetime.fromtimestamp(os.path.getmtime(os.path.join(root,f)))).strftime("%Y-%m-%d %H")
if mtime == d1:
a = open(‘/log/110Hud/ingmss2.txt‘,‘a‘)
a.write(d1+"\n")
a.write(str(mtime)+"\n")
a.close()

                 b = os.path.join(root,f)
                 c = open(‘/log/110Hud/ingmss.txt‘,‘a‘)
                 c.write(d1+"\n")
                 c.write(b.encode(‘utf-8‘)+"\n")
                 c.close()

c = open(‘/log/110Hud/ingmss.txt‘,‘r‘)
for i in c.readlines():
for root,dirs,files in os.walk(i.strip()):
for d in files:
if d == "cc.pac.gz":
e = os.path.join(root,d)
g = open(‘/log/110Hud/ingmss3.txt‘,‘a‘)
g.write(d1+"\n")
g.write(e.encode(‘utf-8‘)+"\n")
g.close()
size = 0
o = open(‘/log/110Hud/ingmss3.txt‘,‘r‘)
for p in o.readlines():
q = os.path.dirname(p)
if not os.path.exists(os.path.join(dst,q)):
os.makedirs(os.path.join(dst,q))
shutil.copy2(os.path.join(dir,p.strip()),os.path.join(dst,q))
b = os.path.getsize(p.strip())
size += int(b)
si = (format((float(size) / 1024 / 1024 / 1024),‘.1f‘))
e = open(‘/log/110Hud/ingmss4.txt‘,‘w‘)
e.write(d1+"\n")
e.write(str(si)+‘G‘)
e.close()

os.system(‘/usr/bin/mail -s "host1(10.0.0.2) ming new data Size at date +%Y-%m-%d_%H:%M" zhi.yang@ming.com < /log/110Hud/ingmss3.txt‘)

:wq

python a1.py

找出1个小时前更新的文件并进行拷贝

标签:get   gms   strftime   tools   文件   目录   dirname   load   拷贝   

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

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