美文网首页
python 删除老的备份文件和web日志

python 删除老的备份文件和web日志

作者: 王宣成 | 来源:发表于2020-08-07 09:11 被阅读0次
    #!/usr/bin/python
    # -*- coding: UTF-8 -*-
    import os
    import shutil
    
    # 环境路径,根据服务器修改
    env_path = "/home/wwwroot/LNMP/"
    
    # 错误日志文件
    err_log_file = env_path + "logs/amh-php-errors.log"
    
    # 删除网站缓存或日志文件
    domain = env_path + "domain/"
    dir_name = 'runtime'
    list_dir = []
    
    # 清空amh错误日志
    if os.path.isfile(err_log_file):
        fo = open(err_log_file, "r+")
        fo.truncate()
    
    
    # 删除多余备份文件
    backup_path = "/home/backup/"
    if not os.path.exists(backup_path):
        os.makedirs(backup_path)
    
    dirs = os.listdir(backup_path)
    find_str = ".tar.gz.amh"
    list_file = []
    
    for file in dirs:
        isfile = os.path.isfile(backup_path + file)
        if isfile is False:
            continue
        if find_str not in file:
            continue
        list_file.append(file)
    
    if len(list_file) > 0:
        list_file.sort()
        last = list_file[-1]
        for i in list_file:
            if i != last:
                os.remove(backup_path + i)
    
    
    # 查找目录名
    def search(path):
        for filename in os.listdir(path):
            fp = os.path.join(path, filename)
            if os.path.isdir(fp):
                if dir_name == filename:
                    list_dir.append(fp)
                else:
                    search(fp)
    
    
    search(domain)
    print(list_dir)
    
    for dir_path in list_dir:
        shutil.rmtree(dir_path)
    
    

    相关文章

      网友评论

          本文标题:python 删除老的备份文件和web日志

          本文链接:https://www.haomeiwen.com/subject/gegxdktx.html