> ## Documentation Index
> Fetch the complete documentation index at: https://e2b-sandbox-agent-sdk-docs.mintlify.site/llms.txt
> Use this file to discover all available pages before exploring further.

# Watch sandbox directory for changes

You can watch a directory for changes using the `files.watchDir()` method in JavaScript and `files.watch_dir()` method in Python.

<Note>
  Since events are tracked asynchronously, their delivery may be delayed.
  It's recommended not to collect or close watcher immediately after making a change.
</Note>

<CodeGroup>
  ```js JavaScript & TypeScript highlight={7-12} theme={null}
  import { Sandbox, FilesystemEventType } from '@e2b/code-interpreter'

  const sandbox = await Sandbox.create()
  const dirname = '/home/user'

  // Start watching directory for changes
  const handle = await sandbox.files.watchDir(dirname, async (event) => {
    console.log(event)
    if (event.type === FilesystemEventType.WRITE) {
      console.log(`wrote to file ${event.name}`)
    }
  })

  // Trigger file write event
  await sandbox.files.write(`${dirname}/my-file`, 'hello')
  ```

  ```python Python highlight={7,12-16} theme={null}
  from e2b_code_interpreter import Sandbox, FilesystemEventType

  sandbox = Sandbox.create()
  dirname = '/home/user'

  # Watch directory for changes
  handle = sandbox.files.watch_dir(dirname)
  # Trigger file write event
  sandbox.files.write(f"{dirname}/my-file", "hello")

  # Retrieve the latest new events since the last `get_new_events()` call
  events = handle.get_new_events()
  for event in events:
    print(event)
    if event.type == FilesystemEventType.WRITE:
      print(f"wrote to file {event.name}")
  ```
</CodeGroup>

## Recursive watching

You can enable recursive watching using the parameter `recursive`.

<Note>
  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.
</Note>

<CodeGroup>
  ```js JavaScript & TypeScript highlight={13,17} theme={null}
  import { Sandbox, FilesystemEventType } from '@e2b/code-interpreter'

  const sandbox = await Sandbox.create()
  const dirname = '/home/user'

  // Start watching directory for changes
  const handle = await sandbox.files.watchDir(dirname, async (event) => {
    console.log(event)
    if (event.type === FilesystemEventType.WRITE) {
      console.log(`wrote to file ${event.name}`)
    }
  }, {
    recursive: true
  })

  // Trigger file write event
  await sandbox.files.write(`${dirname}/my-folder/my-file`, 'hello')
  ```

  ```python Python highlight={7,9} theme={null}
  from e2b_code_interpreter import Sandbox, FilesystemEventType

  sandbox = Sandbox.create()
  dirname = '/home/user'

  # Watch directory for changes
  handle = sandbox.files.watch_dir(dirname, recursive=True)
  # Trigger file write event
  sandbox.files.write(f"{dirname}/my-folder/my-file", "hello")

  # Retrieve the latest new events since the last `get_new_events()` call
  events = handle.get_new_events()
  for event in events:
    print(event)
    if event.type == FilesystemEventType.WRITE:
      print(f"wrote to file {event.name}")
  ```
</CodeGroup>
