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

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

>>>

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值