Search code examples
python-2.7ubuntu-12.04watchdog

python wathdog add more random string to file name


This is my code to watch the folder and file changes

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/ilog@'
    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:

The log I got (when there is a file or folder uploaded by 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

The output here is wathdog add more random string to the end of filename.

and sometime I got event created duplicated .

Anyone know How can I fix this problem?

Thanks


Solution

  • Are you sure this is an error? Many FTP servers first create a temporary file (as marked by your "random characters at the end of the filename") and rename it to the final name once the upload is complete. If this is the case, your Watchdog obviously captures the correct events.