로그 파일 자동 정리

2274 단어 서류

 
#coding=utf-8
#            
import os
import time
import datetime

#               
def delete_file_folder(src):
    '''delete files and folders'''
    if os.path.isfile(src):
        try:
            os.remove(src)
        except:
            pass
    elif os.path.isdir(src):
        for item in os.listdir(src):
            itemsrc=os.path.join(src,item)
            delete_file_folder(itemsrc) 
        try:
            os.rmdir(src)
        except:
            pass

def checktime(yourpath):
	#      
	pathlist=os.listdir(yourpath)
	extlist=['txt','jpg']#         
	for i in range(len(pathlist)):
		source=yourpath+'\\'+pathlist[i]
		dirname=pathlist[i]
		#print dirname
		#print source
		if os.path.isfile(source):
			m=time.localtime(os.stat(source).st_ctime)#       
			startime=datetime.datetime.now()
			endtime=datetime.datetime(m.tm_year,m.tm_mon,m.tm_mday,m.tm_hour,m.tm_min,m.tm_sec) #      
			mydays=(startime-endtime).seconds
			ext=os.path.splitext(source)[1][1:].lower() #    
			if mydays>=10000 and ext in extlist:
				#10000     
				os.remove(source)#      ,   
				print '   ',source,'  '
		#     
		if os.path.isdir(source):
			m=time.localtime(os.stat(source).st_ctime)#       
			startime=datetime.datetime.now()
			endtime=datetime.datetime(m.tm_year,m.tm_mon,m.tm_mday,m.tm_hour,m.tm_min,m.tm_sec) #      
			mydays=(startime-endtime).seconds
			ext=os.path.splitext(source)[1][1:].lower() #    
			if mydays>=10000 and dirname.startswith('201209'):
				#10000     
				delete_file_folder(source)#               
				print '   ',source,'   '

#      
if __name__ == '__main__': 
    try: 
        checktime('E:\\hippo\\target_10.201.76.152')
        checktime('E:\\hippo\\target_FTP_TEST')
        checktime('E:\\ftphome')
    except Exception,se: 
        print str(se);

좋은 웹페이지 즐겨찾기