.

[Python] Log Gathering

by 담배맛구마

생성시간(C_Time)이 전날 06:00부터 오늘 06:00까지의 파일들을 가져온다

import os
import shutil
from time import strftime
from datetime import date, timedelta, datetime

#Get Today and Yesterday
tday = date.today().strftime('%d')
yday = (date.today()-timedelta(1)).strftime('%d')

#Setting################################################################
##[1] Log Directory
logDir = "C:\\LOGS"
##[2] Directory TO Receive Files
dirName = "C:\\"
########################################################################

#Get Time List From Yesterday 06 To Today 05
getLst = ()
for i in range(0, 24):
    if i < 6: getLst += (tday+"0"+str(i),)
    elif i < 10: getLst += (yday+"0"+str(i),)
    else: getLst += (yday+str(i),)

#Calculate Log DirectoryName
logDirSplit = logDir.split("\\")
logDirSplitLen = len(logDirSplit);

#Find Files to send
##[1] Save DirectoryName Having Files
currentDir = ""
##[2] Flag To Check Whether Directory Exists
existDirFlag = 0 
##[3] Set BackupDirectoryName
fullDirName = dirName + "\\###LOGS_BACKUP("+yday+"~"+tday+")"
for (path, dirLst, fileLst) in os.walk(logDir):
    print(path)
    if fileLst:
        for f in fileLst:
            if datetime.fromtimestamp(int(os.stat(path+"\\"+f).st_ctime)).strftime('%d%H') in getLst:
                #Create Directory If Not Exists & Set existDirFlag
                if not existDirFlag:
                    currentDir = fullDirName+"\\"+"\\".join(path.split("\\")[logDirSplitLen:])
                    os.makedirs(currentDir)
                    existDirFlag = 1
                #Do Copy File
                shutil.copy(path+"\\"+f, currentDir)
    existDirFlag = 0


반응형

블로그의 정보

정윤상이다.

담배맛구마

활동하기