1. 程式人生 > >Python 刪除過期檔案

Python 刪除過期檔案

一、清理本地檔案

import datetime
from utils import confUtils, hadoop
import os
import shutil
import time
import sys


def cleandir(path, duration):
    time_now = time.time()
    for root, dirs, files in os.walk(path):
        for name in files:
            mtime = os.stat(os.path.join(root, name)).st_mtime
            if time_now - mtime > 3600 * 24 * duration:
                print name
                os.remove(os.path.join(root, name))
        for dirname in dirs:
            if (datetime.datetime.now() - datetime.datetime.strptime(dirname , '%Y%m%d')).days > duration:
                print dirname
                shutil.rmtree(os.path.join(root, dirname))

二、清除HDFS檔案

def clean_hdfs(hdfs_path, duration):
    log_time = (datetime.datetime.now() - datetime.timedelta(days=duration)).strftime('%Y%m%d')
    hadoop.hdfs_rm('%s/%s' % (hdfs_path, log_time), recursive=True)