[Python] 纯文本查看 复制代码
#coding = utf-8
import os
import pyinotify
import logging
# LOG class
class classlog(object):
"""log class"""
def __init__(self,logfilename="/tmp/log.txt",level="INFO"):
level = level if level in ['CRITICAL','ERROR','WARNING','INFO','DEBUG','NOTSET'] else 'INFO'
self.logger = logging.getLogger("classlog")
self.logger.setLevel(logging.DEBUG)
Fileformatter = logging.Formatter("%(asctime)s - %(levelname)-8s:%(message)s",
datefmt='%Y-%m-%d %I:%M:%S %p')
Streamformatter = logging.Formatter("%(asctime)s %(levelname)s:%(message)s",
datefmt='%Y-%m-%d %I:%M:%S')# ,filename='example.log')
Filelog = logging.FileHandler(logfilename)
Filelog.setFormatter(Fileformatter)
Filelog.setLevel(logging.DEBUG)
Streamlog = logging.StreamHandler()
Streamlog.setFormatter(Streamformatter)
Streamlog.setLevel(level)
self.logger.addHandler(Filelog)
self.logger.addHandler(Streamlog)
def debug(self,msg):
self.logger.debug(msg)
def info(self,msg):
self.logger.info(msg)
def warn(self,msg):
self.logger.warn(msg)
def error(self,msg):
self.logger.error(msg)
def critical(self,msg):
self.logger.critical(msg)
class pyinotify_status(pyinotify.ProcessEvent):
def process_IN_CREATE(self,event):
LOG.info("CREATE file: %s"% os.path.join(event.path,event.name))
# reg=re.compile(r"\( \)")
# cpfile=reg.sub('',event.pathname)
# print cpfile
os.system("cp %s /tmp/"% event.pathname)
os.system("rm -rf %s"% event.pathname)
def process_IN_DELETE(self, event):
LOG.info("DELETE file: %s" % event.pathname)
def process_IN_MODIFY(self, event):
LOG.info("MODIFY file: %s" % event.pathname)
def main():
wm=pyinotify.WatchManager()
path=raw_input("please input path:")
wm.add_watch(path,pyinotify.ALL_EVENTS,rec=True)
eh=pyinotify_status()
notifier = pyinotify.Notifier(wm, eh)
notifier.loop()
if __name__=='__main__':
LOG = classlog("/tmp/log.txt")
main()