Python监控文件夹 && 发送邮件
直接上代码:
# python3
# -*- coding: utf-8 -*-
# 2017/06/16 by luohan from email.mime.text import MIMEText
from email.header import Header
from email.utils import parseaddr, formataddr
import smtplib
import time import os def get_size(start_path = '.'):
total_size = 0
for dirpath, dirnames, filenames in os.walk(start_path):
for f in filenames:
fp = os.path.join(dirpath, f)
total_size += os.path.getsize(fp)
return total_size / (1024 * 1024 * 1024) def _format_addr(s):
name, addr = parseaddr(s)
return formataddr((Header(name, 'utf-8').encode(), addr)) def send_mail():
from_addr = '**@**.com'
passwd = '***'
to_addrs = ['123@qq.com', '234@qq.com'] msg = MIMEText('统计服务器/dev/shm内存报警', 'plain', 'utf-8')
msg['From'] = from_addr
msg['To'] = ','.join(to_addrs)
msg['Subject'] = Header('线上服务器报警', 'utf-8').encode() try:
smtp_server = 'smtp.exmail.qq.com'
server = smtplib.SMTP_SSL(smtp_server, 465)
server.login(from_addr, passwd)
server.sendmail(from_addr, to_addrs, msg.as_string())
with open('/home/jobs/mail.log', 'a') as f:
print('{}: send success'.format(time.time()), file=f)
except smtplib.SMTPException as e:
with open('/home/jobs/mailerr.log', 'a') as f:
print('{}: send failed, {}'.format(time.time(), e), file=f)
finally:
server.quit() def check_dir_size(target_dir):
dirsize = get_size(target_dir)
# 超过12G,总大小16G
if dirsize > 12:
send_mail() check_dir_size('/dev/shm')
参考资料:
Python监控文件夹 && 发送邮件的更多相关文章
- Python 的 pyinotify 模块 监控文件夹和文件的变动
官方参考: https://github.com/seb-m/pyinotify/wiki/Events-types https://github.com/seb-m/pyinotify/wiki/I ...
- Python监控文件变化:watchdog
Python监控文件变化有两种库:pyinotify和watchdog.pyinotify依赖于Linux平台的inotify,后者则对不同平台的的事件都进行了封装.也就是说,watchdog跨平台. ...
- python 遍历文件夹 文件
python 遍历文件夹 文件 import os import os.path rootdir = "d:\data" # 指明被遍历的文件夹 for parent,dirn ...
- python 关于文件夹的操作
在python中,文件夹的操作主要是利用os模块来实现的, 其中关于文件夹的方法为:os.lister() , os.path.join() , os.path.isdir() # path 表示文 ...
- Python打包文件夹的方法小结(zip,tar,tar.gz等)
本文实例讲述了Python打包文件夹的方法.分享给大家供大家参考,具体如下: 一.zip ? 1 2 3 4 5 6 7 8 9 10 11 import os, zipfile #打包目录为zip文 ...
- python操作txt文件中数据教程[3]-python读取文件夹中所有txt文件并将数据转为csv文件
python操作txt文件中数据教程[3]-python读取文件夹中所有txt文件并将数据转为csv文件 觉得有用的话,欢迎一起讨论相互学习~Follow Me 参考文献 python操作txt文件中 ...
- Storm监控文件夹变化 统计文件单词数量
监控指定文件夹,读取文件(新文件动态读取)里的内容,统计单词的数量. FileSpout.java,监控文件夹,读取新文件内容 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 ...
- 【.Net 学习系列】-- FileSystemWatcher 监控文件夹新生成文件,并在确认文件没有被其他程序占用后将其移动到指定文件夹
监控文件夹测试程序: using System; using System.Collections.Generic; using System.IO; using System.Linq; using ...
- 用Python打开文件夹
用Python读取文件夹, 然后打开文件 下面读取到文件的每一个内容, 然后加上路径 import os path = r'../Downloads/text/content' for filenam ...
随机推荐
- ln -s 软链接命令
所有对软链接link_name的操作都是对目录或文件dir_file的操作 ln -s [dir_file] [link_name]
- 搭建vue2.0开发环境及手动安装vue-devtools工具
安装vue脚手架 1.安装node.js,如果安装成功输入 node -v ,查看node版本号,输入npm -v查看npm版本 https://nodejs.org/en/ 2.注册淘宝镜像,定制的 ...
- 一、uart&tty驱动
一.I.MX6 UART驱动 文件路径:\linux_IMX6_CoreC_3..35_for_Linux\drivers\tty\serial\imx.c .驱动入口函数:imx_serial_in ...
- svg整体缩放至指定大小
一.问题 svg画面跑在分辨率低的电脑上,导致不能完全显示. 二.要求 svg要能够根据电脑的屏幕大小自动缩放至适配电脑的尺寸. 三.实现 1.获取本机窗口高度.宽度 let clientWidth ...
- I - Fill The Bag codeforces 1303D
题解:注意这里的数组a中的元素,全部都是2的整数幂.然后有二进制可以拼成任意数.只要一堆2的整数幂的和大于x,x也是2的整数幂,那么那一堆2的整数幂一定可以组成x. 思路:位运算,对每一位,如果该位置 ...
- springboot集成swagger2多模块中文配置详细步骤,解决集成mybatis或mybatis-plus无法正常使用问题
pom.xm里写入swagger依赖: <dependency> <groupId>io.springfox</groupId> <artifactId> ...
- testNG 问题总结
1. Eclipse中TestNG报告乱码问题 在eclipse 安装根目录下的eclipse.ini 文件,在最后增加 -Dfile.encoding=UTF-8
- Mybatis Generator通用Join的实现
通常,我们使用Mybatis实现join表关联的时候,一般都是通过在xml或注解里写自定义sql实现. 本文通过Mybatis Generator的插件功能新增一个JoinPlugin插件,只要在配置 ...
- 大数据Hbase相关运维题
1.启动先电大数据平台的 Hbase 数据库,其中要求使用 master 节点的RegionServer.在 Linux Shell 中启动 Hbase shell,查看 HBase 的版本信息.(相 ...
- 用 Python 获取百度搜索结果链接
前言 近期有许多项目需要这个功能,由于Python实现起来比较简单就这么做了,代码贴下来觉得好点个赞吧~ 代码 # coding: utf-8 import os import time import ...