2014-09-01 67 views
1

這是我的代碼看的文件夾和文件更改蟒蛇wathdog增加隨機字符串文件名

import sys 
import time 
import logging 
from watchdog.observers import Observer 
from watchdog.events import LoggingEventHandler 
from watchdog.events import FileSystemEventHandler 
class MyHandler(FileSystemEventHandler): 
    LOG = '/var/www/time/logs/server_logs/[email protected]' 
    def on_created(self, event): 
     timestr = time.strftime("%Y%m%d") 
     log = open(self.LOG+timestr,'aw+') 
     log.write(event.src_path +"##"+time.strftime('%Y-%m-%d %H:%M:%S')+"##created\n") 


if __name__ == "__main__": 
    path = "/ftpo/work" 
    event_handler = MyHandler() 
    observer = Observer() 
    observer.schedule(event_handler, path, recursive=True) 
    observer.start() 
    try: 
     while True: 

我得到的日誌(當有通過FTP上傳文件或文件夾)

/ftpo/work/emergency77r83j90s933/a detourer/site white BG/Barcelona.jpg##2014/09/01 17:35:56##created 
/ftpo/work/bal1/a detourer/Tarif - C/Det pour le 02-09_C##2014/09/01 17:40:13##created 
/ftpo/work/bal1/a detourer/Tarif - C/Det pour le 02-09_C/.AppleDouble##2014/09/01 17:40:13##created 
/ftpo/work/bal1/a detourer/..DS_Store.VpiV3e##2014/09/01 17:40:13##created 
/ftpo/work/bal1/a detourer/Tarif - C/..DS_Store.Y7c8E1##2014/09/01 17:40:13##created 
/ftpo/work/bal1/a detourer/Tarif - C/.AppleDouble/..Parent.b39ogO##2014/09/01 17:40:13##created 
/ftpo/work/bal1/a detourer/Tarif - C/Det pour le 02-09_C/..DS_Store.09McRB##2014/09/01 17:40:14##created 
/ftpo/work/bal1/a detourer/Tarif - C/Det pour le 02-09_C/.Creature-0008.eps.j8ocAp##2014/09/01 17:40:14##created 
/ftpo/work/bal1/a detourer/Tarif - C/Det pour le 02-09_C/.AppleDouble/..DS_Store.7hRSo1##2014/09/01 17:41:19##created 
/ftpo/work/bal1/a detourer/Tarif - C/Det pour le 02-09_C/.AppleDouble/..Parent.uC5AdD##2014/09/01 17:41:19##created 
/ftpo/work/bal1/a detourer/Tarif - C/Det pour le 02-09_C/.AppleDouble/.Creature-0008.eps.ORQk2e##2014/09/01 17:41:19##created 

這裏的輸出是wathdog,將更多的隨機字符串添加到文件名的末尾。

有時候我創建了重複的事件。

任何人都知道我該如何解決這個問題?

謝謝

回答

1

您確定這是?許多FTP服務器首先創建一個臨時文件(由「文件名末尾的隨機字符」標記),並在上傳完成後將其重命名爲最終名稱。如果是這種情況,你的看家狗顯然會捕獲正確的事件。