python watchdog

监视文件变更

#!/usr/bin/python
# -*- coding:UTF-8 -*-

import time
from watchdog.observers import Observer
from watchdog.events import RegexMatchingEventHandler

class MyHandler(RegexMatchingEventHandler):

    def __init__(self, regex_list=[r".*"]):
        super(MyHandler, self).__init__(regex_list)

    def on_created(self, event):
        if event.is_directory:
            pass
        else:
            print(event.event_type, event.src_path)

    def on_deleted(self, event):
        if event.is_directory:
            pass
        else:
            print(event.event_type, event.src_path)

    def on_modified(self, event):
        if event.is_directory:
            pass
        else:
            print(event.event_type, event.src_path)

    def on_moved(self, event):
        print("move", event.src_path, event.dest_path)

if __name__ == "__main__":
    reges = [r".*\.c", r".*\.h", r".*\.cpp"]
    event_handler = MyHandler(reges)
    observer = Observer()
    observer.schedule(event_handler, ".", recursive=True)
    observer.start()

    try:
        print("start my watch")
        while True:
            time.sleep(100)
    except KeyboardInterrupt:
        observer.stop()
    observer.join()

读取配置文件

# -*- coding: utf-8

from configparser import ConfigParser

def get_config(section_name="env", conf_file="ssh-config.ini"):
    '''
    :param section_name:
    :param conf_file:
    :return dictornary:

    eg.
    [env]
    user = root
    password = root123

    return {"user":"root", "password":"root123"}
    '''
    config = ConfigParser()
    config.read_file(open(conf_file))
    return dict(config.items(section_name))


for k, v in get_config().items():
    print(k, ":", v)
posted @ 2015-06-22 22:16  Let it be!  阅读(1897)  评论(0编辑  收藏  举报