Locate and copy the files that were updated 1 hours ago:
Note:
1, according to the time stamp judgment, not 60 minutes
2. Os.makedirs Create directory path recursively
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) to 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
"[email protected] </log/110hud/ingmss3.txt ')
: Wq
Python a1.py
Find and copy files that were updated 1 hours ago