You can watch a directory for changes using the files.watchDir() method in JavaScript and files.watch_dir() method in Python.
Since events are tracked asynchronously, their delivery may be delayed.
It’s recommended not to collect or close watcher immediately after making a change.
from e2b import Sandbox, FilesystemEventTypesandbox = Sandbox.create()dirname = '/home/user'# Watch directory for changeshandle = sandbox.files.watch_dir(dirname)# Trigger file write eventsandbox.files.write(f"{dirname}/my-file", "hello")# Retrieve the latest new events since the last `get_new_events()` callevents = handle.get_new_events()for event in events: print(event) if event.type == FilesystemEventType.WRITE: print(f"wrote to file {event.name}")
You can enable recursive watching using the parameter recursive.
When rapidly creating new folders (e.g., deeply nested path of folders), events other than CREATE might not be emitted. To avoid this behavior, create the required folder structure in advance.
from e2b import Sandbox, FilesystemEventTypesandbox = Sandbox.create()dirname = '/home/user'# Watch directory for changeshandle = sandbox.files.watch_dir(dirname, recursive=True)# Trigger file write eventsandbox.files.write(f"{dirname}/my-folder/my-file", "hello")# Retrieve the latest new events since the last `get_new_events()` callevents = handle.get_new_events()for event in events: print(event) if event.type == FilesystemEventType.WRITE: print(f"wrote to file {event.name}")