python定期清理過期資料

2021-09-29 03:29:37 字數 1781 閱讀 2075

功能:

定期清理資料夾中的過期資料

#!/usr/bin/python3

# -*- coding:utf-8 -*-

import os

import time

import datetime

import shutil

import socket

import logging

import sys

#獲取主機名

hostname = socket.gethostname()

print (hostname)

source_path = "/data/tomcat8_publish/temp"

datetime = time.strftime('%y%m%d%h%m%s')

time = time.strftime('%y-%m-%d %h:%m:%s')

#定義日誌級別

logging.basicconfig(level=logging.debug,filename='/data/del_data/log_file_del.log',filemode='a',format='%(asctime)s - %(levelname)s: %(message)s')

#執行清空30天以前的打包資料

def run_rm():

logging.info('###############開始檢測是否有乙個月之前的備份資料################')

f = list(os.listdir(source_path))

# 獲取當前時間

today=datetime.datetime.now()

# 計算偏移量,前30天以前的資料清理掉

offset=datetime.timedelta(days=-30)

re_date = (today + offset)

# 前30天時間轉換為時間戳

re_date_unix = time.mktime(re_date.timetuple())

for i in f:

file_path = os.path.join(source_path,i)

file_time = os.path.getmtime(file_path)

timearray = time.localtime(file_time)

otherstyletime = time.strftime("%y-%m-%d %h:%m:%s", timearray)

if file_time <= re_date_unix:

if os.path.isdir(file_path):

logging.info('已經超過30天,刪除%s下的%s成功!' % (tar_backup_path,i))

shutil.rmtree(file_path)

else:

logging.info('已經超過30天,刪除%s下的%s成功!' % (tar_backup_path,i))

os.remove(file_path)

if __name__ == '__main__':

logging.info("指令碼執行開始at:%s" % time)

run_rm()

logging.info("指令碼執行結束at:%s" % time.strftime('%y-%m-%d %h:%m:%s'))

好了,這就是python定期清理過期資料的方法了,如有問題可與博主一起交流討論!

mysql定時任務event 清理過期資料

需要刪除資料的表名 t req log 建表sql create table t req log id bigint 20 not null auto increment,host varchar 200 default null time datetime default null primary...

定期刪除Hive表的過期資料

由於hive中有很多表都是每日全量的,資料量比較大,有些是可以將過去歷史分割槽的資料進行刪除的,所以需要乙個定時執行的指令碼,定時刪除前七天的過期資料。注 此指令碼只針對分割槽欄位為日期型別 bin bash today date y m d today timestamp date d today...

處理redis過期資料

可以採用三種方法 定時刪除 定期刪除 惰性刪除 一 定時刪除 寫乙個定時器,將key的過期時間到達時,立刻將鍵刪除 優點 節約記憶體 缺點 增加cpu壓力,影響redis伺服器響應時間和吞吐量 二 定期刪除 activeexpirecycle 函式對每個expires 資料庫 逐一進行檢測 對每個資...