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
>>>