滚动备份日志
#encoding:utf-8
#usr/bin/ python
import time
import os
from datetime import datetime

# 字节bytes转化kb
def formatSize(bytes):
    try:
        bytes = float(bytes)
        kb = bytes / 1024
    except:
        print("传入的字节格式不对")
        return "Error"

    return kb

def check(path, cnt):
    try:
        size = os.path.getsize(path)
        kb_size = formatSize(size)
        if kb_size > 50*1024:
            os.system('cp ' + path + ' ' + path.split('.')[0] + "_history_" + str(cnt) +'.txt' )
            print "Copy at " + time.strftime("%Y %m %d %H:%M:%S", time.localtime())
            os.system('rm ' + path):
            return
    except Exception as err:
        print(err)
    pass


if __name__ == '__main__':
    cnt = 1
    while True:
        path = "filepath"
        check(path, cnt)
        time.sleep(60*30)
        cnt += 1
Logo

CSDN联合极客时间,共同打造面向开发者的精品内容学习社区,助力成长!

更多推荐