码迷,mamicode.com
首页 > 其他好文 > 详细

监测目录文件下文件的生成 修改

时间:2017-02-17 12:46:29      阅读:189      评论:0      收藏:0      [点我收藏+]

标签:目录文件   err   eric   stream   ast   rom   types   tree   文件   

#coding=utf-8
import os,logging,ctypes
import win32file
import win32con
import logging
from logging.handlers import TimedRotatingFileHandler
from radar_operation import RADAR_OPERATION
RADAR_OPERATION = RADAR_OPERATION()

FOREGROUND_WHITE = 0x0007
FOREGROUND_BLUE = 0x01 # text color contains blue.
FOREGROUND_GREEN= 0x02 # text color contains green.
FOREGROUND_RED = 0x04 # text color contains red.
FOREGROUND_YELLOW = FOREGROUND_RED | FOREGROUND_GREEN
 
STD_OUTPUT_HANDLE= -11
std_out_handle = ctypes.windll.kernel32.GetStdHandle(STD_OUTPUT_HANDLE)
def set_color(color, handle=std_out_handle):
 bool = ctypes.windll.kernel32.SetConsoleTextAttribute(handle, color)
 return bool
 
class Logger:
    def __init__(self, path,clevel = logging.DEBUG,Flevel = logging.DEBUG):
      self.logger = logging.getLogger(path)
      self.logger.setLevel(logging.DEBUG)
      fmt = logging.Formatter(‘%(asctime)s->%(filename)s->%(module)s->%(lineno)d->%(levelname)s->%(message)s ‘, ‘%Y-%m-%d %H:%M:%S‘)
      #设置CMD日志
      sh = logging.StreamHandler()
      sh.setFormatter(fmt)
      sh.setLevel(clevel)
      #设置文件日志
      #fh = logging.FileHandler(path)
      fh = TimedRotatingFileHandler(filename=path, when="M", interval=30, backupCount=2)
      fh.setFormatter(fmt)
      fh.setLevel(Flevel)
      self.logger.addHandler(sh)
      self.logger.addHandler(fh)
 
    def debug(self,message):
      self.logger.debug(message)
 
    def info(self,message):
        self.logger.info(message)
 
    def war(self,message,color=FOREGROUND_YELLOW):
      set_color(color)
      self.logger.warn(message)
      set_color(FOREGROUND_WHITE)
 
    def error(self,message,color=FOREGROUND_RED):
      set_color(color)
      self.logger.error(message)
      set_color(FOREGROUND_WHITE)
 
    def cri(self,message):
        self.logger.critical(message)

def mymonitor(path_to_watch):
    ‘‘‘
    监控directory目录,对该目录下新曽或修改文件进行自动传输
    ‘‘‘
    logf = Logger("main.log",logging.WARNING,logging.DEBUG)

    ACTIONS = {  
          1 : "Created",
          # 2 : "Deleted",  
          3 : "Updated"
          # 4 : "Renamed from something",  
          # 5 : "Renamed to something"  
        }  

    FILE_LIST_DIRECTORY = win32con.GENERIC_READ | win32con.GENERIC_WRITE
    hDir = win32file.CreateFile (  
          path_to_watch,  
          FILE_LIST_DIRECTORY,  
          win32con.FILE_SHARE_READ | win32con.FILE_SHARE_WRITE,  
          None,  
          win32con.OPEN_EXISTING,  
          win32con.FILE_FLAG_BACKUP_SEMANTICS,  
          None  
        )  
    while 1:
        results = win32file.ReadDirectoryChangesW (  
               hDir,  #handle: Handle to the directory to be monitored. This directory must be opened with the FILE_LIST_DIRECTORY access right.  
               1024,  #size: Size of the buffer to allocate for the results.  
               True,  #bWatchSubtree: Specifies whether the ReadDirectoryChangesW function will monitor the directory or the directory tree.   
               win32con.FILE_NOTIFY_CHANGE_FILE_NAME,
                # win32con.FILE_NOTIFY_CHANGE_DIR_NAME |  
                # win32con.FILE_NOTIFY_CHANGE_ATTRIBUTES |  
                # win32con.FILE_NOTIFY_CHANGE_SIZE,
                # win32con.FILE_NOTIFY_CHANGE_LAST_WRITE |  
                # win32con.FILE_NOTIFY_CHANGE_SECURITY,  
                #修改这里可以控制一个文件监控到后输出多个信息的问题
               None,  
               None)  
        for action,file in results:
            full_filename = os.path.join(path_to_watch,file)
            if os.path.isfile(full_filename):
                print ‘=================================‘
                print ‘monited‘,full_filename,action
                logf.info(‘receive ‘+full_filename)
                # RADAR_OPERATION.Level3_products_from_single_radar(full_filename,"")
                try:
                    RADAR_OPERATION.test(full_filename)
                except:
                    print ‘-------------------‘
if __name__ == "__main__":
    path_to_watch = r"E:\radar_operation\test_bed\CIMISS_IN"
    mymonitor(path_to_watch)

监测目录文件下文件的生成 修改

标签:目录文件   err   eric   stream   ast   rom   types   tree   文件   

原文地址:http://www.cnblogs.com/xiaoxiaoshuaishuai0219/p/6409292.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!