美文网首页
python判断根目录大小进行清理out格式文件

python判断根目录大小进行清理out格式文件

作者: 小黑佬 | 来源:发表于2020-09-07 16:59 被阅读0次
import os
import logging
import psutil



logging.basicConfig(
        filename='app.log',
        level=logging.INFO,
        format='%(levelname)s:%(asctime)s:%(message)s'
    )


def purge_nohup() :
    for root, dirs, files in os.walk('/home/linux', topdown=False):
        for name in files:
            # print(name)
            # 判断是否包含log
            if "out" in name and "nohup" in name:
                print(os.path.join(root, name))
                with open(os.path.join(root, name),'w') as f:
                    print(os.stat(os.path.join(root, name)).st_size)
                    print(os.path.getsize(os.path.join(root, name)))
                    logging.info('the file   %s is null ', os.path.join(root, name))
                # full_path_name = os.path.join(root, name)
                # print(full_path_name)
                #判断日期
    #    for name  in dirs:
    #        print(name)    

disk_usage=psutil.disk_usage('/')
print("硬盘根目录使用率为%s" % disk_usage.percent)
disk_usage=int(disk_usage.percent)
print("硬盘根目录使用率为%s" % disk_usage)
if disk_usage > 5:
    purge_nohup()

这句 if "out" in name and "nohup" in name: ,其实可以把关键字匹配改为标准的文件后缀名匹配会更加准确。

>>> filename = 'spam.txt'
>>> filename.endswith('.txt')
True
>>> filename.startswith('file:')
False
>>> url = 'http://www.python.org'
>>> url.startswith('http:')
True
>>>

相关文章

网友评论

      本文标题:python判断根目录大小进行清理out格式文件

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