找出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()
#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(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
" [email protected] < /log/110Hud/ingmss3.txt‘)
:wq
python a1.py
找出1個小時前更新的文件並進行拷貝