python 实现保存最新的三份文件,其余的都删掉

yipeiwu_com6年前Python基础

我就废话不多说了,直接上代码吧!

"""
对于每天存储文件,文件数量过多,占用空间
采用保存最新的三个文件
"""
from airflow import DAG
from airflow.operators.python_operator import PythonOperator
from airflow.models import Variable
from sctetl.airflow.utils import dateutils
from datetime import datetime,timedelta
import logging
import os
import shutil
"""
base_dir = "/data"
data_dir = "/gather"
"gather下边存在不同的文件夹"
"/data/gather/test"
"test路径下有以下文件夹"
"20180812、20180813、20180814、20180815、20180816"
"""
 
base_dir = Variable.get("base_dir")
data_dir = Variable.get("data_dir")
keep = 3
 
default_arg = {
  "owner":"airflow",
  "depends_on_past":False,
  "start_date":dateutils.get_start_date_local(2018,8,27,18,5),
  "email":[''],
  "email_on_failure":False,
  "email_on_retry":False,
  "retries":1,
  "retry_delay":timedelta(minutes=5)
}
 
dag = DAG(dag_id="keep_three_day",default_args=default_arg,schedule_interval=dateutils.get_schedule_interval_local(18,5))
 
def keep_three_day():
  path = os.path.join(base_dir, data_dir)
  date_cates = os.listdir(path)
  for cate in date_cates:
    p = os.path.join(base_dir, data_dir, cate)
    if os.path.isdir(p):
      dir_names = os.listdir(p)
      dir_names.sort()
      for i in dir_names[:-keep]:
        logging.info("删除目录 {path}".format(path=os.path.join(p, i)))
        shutil.rmtree(os.path.join(p, i))
 
with dag:
  keep_three_file = PythonOperator(task_id="keep_three_file",python_callable=keep_three_day(),dag=dag)
 
keep_three_file
 

以上这篇python 实现保存最新的三份文件,其余的都删掉就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持【听图阁-专注于Python设计】。

相关文章

python difflib模块示例讲解

python difflib模块示例讲解

difflib模块提供的类和方法用来进行序列的差异化比较,它能够比对文件并生成差异结果文本或者html格式的差异化比较页面,如果需要比较目录的不同,可以使用filecmp模块。 clas...

对Python 除法负数取商的取整方式详解

python除法负数商的取整方式与C++不同 python: 5 / -2 = -3 若想和C++行为相同,可以使用 int(operator.truediv(num1, num2...

TensorFlow实现模型评估

TensorFlow实现模型评估

我们需要评估模型预测值来评估训练的好坏。 模型评估是非常重要的,随后的每个模型都有模型评估方式。使用TensorFlow时,需要把模型评估加入到计算图中,然后在模型训练完后调用模型评...

在Python中os.fork()产生子进程的例子

例1 import os print 'Process (%s) start...' %os.getpid() pid = os.fork() if pid==0: print...

python中的计时器timeit的使用方法

本文介绍了python中的计时器timeit的使用方法,分享给大家,具体如下: timeit 通常在一段程序的前后都用上time.time(),然后进行相减就可以得到一段程序的运行时间...