Reputation: 931
Renaming a file that is being monitored in watchdog produces a on_moved event trigger. The problem I'm having is that there is no way to tell what the file was moved/renamed to (as the on_moved event trigger also happens when a file was renamed). Is there any way this is built into watchdog or should I build a workaround in the program I'm writing?
Here's some sample code
#!/usr/bin/python
'''
Created on 2014-07-03
'''
import sys
import time
from watchdog.observers import Observer
from watchdog.events import FileSystemEventHandler
'''
Extend FileSystemEventHandler to be able to write custom on_any_event method
'''
class MyHandler(FileSystemEventHandler):
'''
Overwrite the methods for creation, deletion, modification, and moving
to get more information as to what is happening on output
'''
def on_created(self, event):
print("created: " + event.src_path)
def on_deleted(self, event):
print("deleted: " + event.src_path)
def on_modified(self, event):
print("modified: " + event.src_path)
def on_moved(self, event):
print("moved/renamed: " + event.src_path)
watch_directory = sys.argv[1] # Get watch_directory parameter
event_handler = MyHandler()
observer = Observer()
observer.schedule(event_handler, watch_directory, True)
observer.start()
'''
Keep the script running or else python closes without stopping the observer
thread and this causes an error.
'''
try:
while True:
time.sleep(1)
except KeyboardInterrupt:
observer.stop()
observer.join()
The code prints out whenever an event happens, which type of event happened and the path to the file/folder. It takes one parameters which is the path to the folder to be watched.
Upvotes: 6
Views: 5477
Reputation: 494
if you don't know what are the methods and properties an object have
just do print dir(obj)
here in your case event.dest_path
will do the work
Upvotes: 10