Today received a call, there is a downstairs customer response, the data has a problem, I connected to the server, found broke a disk character space is full, resulting in, application in error, stop service, delete a huge log file, service opened, all normal.
Long wanted to write a script on the server, today is just empty, so ba la a bit, wrote a small Python script, Daniel does not have to read, the rationale is to find out the modification of each file time, if the modification time is not the same day, if the change is less than today, then we delete this file . Write a coarser, look at the code:
#!/usr/bin/env pythonimport osimport shutilimport timeimport datetimepath1= ' c:\\backup_queue_broker_shtumor_ 20130218\\log\\ ' #path2 = ' c:\\backup_other_broker_shtumor_20130218\log\\ ' #path3 = ' c:\\backup_schedule_broker_ Shtumor_20130807\\log\\ ' def deletefile (path): Today=datetime.datetime.today () file=os.listdir (path) for I in file: Mtime=os.stat (path+i). St_mtime #得到文件的修改时间标记format = '%y-%m-%d ' value=time.localtime (mtime) dt=time.strftime (format, Value) Dt1=datetime.datetime.strptime (dt,format) #转换下时间格式today = (str (today)) [: 10]dt1=str (DT1) [: ten] #print dt1#print Todayif Dt1 < today:p rint "Begin to Delete File", Path+ios.remove (path+i) else:passdeletefile (path1) #deleteFile ( path2) #deleteFile (PATH3)
Hehe, finally bottled into a function, should have multiple paths of files need to be deleted, if there is a better way, please do not enlighten us ...
This article is from "You are a passer-by or fearless" blog, please be sure to keep this source http://world77.blog.51cto.com/414605/1576599
Let python delete the file under window