I am trying to watch some text files for when they are modified using watchdog
but I only seem to get events for .tmp
files. I understand this is how sublime text is saving files, but shouldn't I also get an event fired for the actual file too?
This is what I get when trying to save a file at the location /home/john/resources/css/style.css
in sublime text:
/home/john/resources/css/.sublaa.tmp
/home/john/resources/css/.sublaa.tmp
/home/john/resources/css/.sublaa.tmp
It seems I only get events fired for the tmp files, but not for the actual file. This actually works fine on MacOSX, but not Ubuntu.
#!/usr/bin/python
import time
from watchdog.observers import Observer
from watchdog.events import FileSystemEventHandler
class MyHandler(FileSystemEventHandler):
def on_any_event(self, event):
print event.src_path
if __name__ == "__main__":
event_handler = MyHandler()
observer = Observer()
observer.schedule(event_handler, path='.', recursive=False)
observer.start()
try:
while True:
time.sleep(1)
except KeyboardInterrupt:
observer.stop()
observer.join()
This is a common problem with editors that create temporary files. In the watchdog package page ( https://pypi.python.org/pypi/watchdog ) you can find this note regarding vim:
About using watchdog with editors like Vim
Vim does not modify files unless directed to do so. It creates backup files and then swaps them in to replace the files you are editing on the disk. This means that if you use Vim to edit your files, the on-modified events for those files will not be triggered by watchdog. You may need to configure Vim to appropriately to disable this feature.
In Sublime to disable the creation of tmp files you must go to Preferences --> Settings-User and disable atomic saves.
"atomic_save": false
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With