フォルダ監視をする必要があるときは
大抵、time?かなにかで時限式に
ls -la をgrepして判定するんですが
これができるのはUNIX系だけです。
WINDOWSは調べるのめんどそお
で、Pythonで組んでみる

簡単なやつでファイル構成が変わるか
最終更新日が変わったかで見るものです。

import os,sys,time,pprint
from stat import *


class touched:
  def watching(self,dirpath):
    
    dir1 = self.dirstat(dirpath)
    
    while 1:
      
      time.sleep(5)
    
      try:
        dir2 = self.dirstat(dirpath)
        self.checkdirf(dir1,dir2)
        self.checkdirs(dir1,dir2)
        print "next ... "
      except Exception,e:
        print e
        
      dir1 = dir2[:]
    
  def checkdirf(self,dir1,dir2):
    
    if dir1[0] != dir2[0]:
      raise Exception("FILE_DIFF")
    
  def checkdirs(self,dir1,dir2):
    
    if len(dir1[0]) == 0 or len(dir2[0]) == 0:
      raise Exception("NO FILE")
    
    pp = pprint.PrettyPrinter(indent=4) 
    for i in range(0,len(dir1)):
      # pp.pprint(dir1[1])
      if dir1[1][i][ST_MTIME] != dir2[1][i][ST_MTIME]:
        raise Exception("UPDATED")
      
    
  def dirstat(self,dirpath):
    tmp = os.listdir(dirpath)
    tmp = tmp[:]
    retdir = []
    
    if len(tmp) != 0:
      tmp.sort()
      for item in tmp:
        tmpstat = os.stat(os.path.join(dirpath,item))[:]
        retdir.extend([tmpstat])
      
    return [tmp,retdir]
    
    

test = touched()
test.watching("c:/test")

タグ:

+ タグ編集
  • タグ:

このサイトはreCAPTCHAによって保護されており、Googleの プライバシーポリシー利用規約 が適用されます。

最終更新:2007年01月16日 00:32