> ## Documentation Index
> Fetch the complete documentation index at: https://novita.ai/docs/llms.txt
> Use this file to discover all available pages before exploring further.

# Sandbox-Verzeichnis auf Änderungen überwachen

export const SandboxConfigHint = () => {
  if (typeof document === "undefined") {
    return null;
  } else {
    return <Note>Before running the example code in this document, please ensure you have properly configured environment variables. For details, please refer to <a href="/docs/de/guides/sandbox-your-first-agent-sandbox#configure-environment-variables">Configure Environment Variables</a>.</Note>;
  }
};

Du kannst die Methode `files.watchDir()` in JavaScript und die Methode `files.watch_dir()` in Python verwenden, um ein Verzeichnis auf Änderungen zu überwachen.

<Note>
  Da Ereignisse asynchron verfolgt werden, kann ihre Zustellung verzögert sein.
  Es wird empfohlen, den Watcher nicht unmittelbar nach einer Änderung zu erfassen oder zu schließen.
</Note>

<SandboxConfigHint />

<CodeGroup>
  ```js JavaScript & TypeScript icon="js" theme={"system"}
  import { Sandbox, FilesystemEventType } from 'novita-sandbox/code-interpreter'

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

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

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

  // Stop watching directory for changes
  handle.stop()

  await sandbox.kill()
  ```

  ```python Python icon="python" theme={"system"}
  from novita_sandbox.code_interpreter import Sandbox, FilesystemEventType

  sandbox = Sandbox.create()
  dirname = '/tmp'

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

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

  # Stop watching directory for changes
  handle.stop()

  sandbox.kill()
  ```
</CodeGroup>

## Rekursives Überwachen

Du kannst den Parameter `recursive` festlegen, um rekursives Überwachen zu aktivieren.

<Note>
  Wenn neue Ordner schnell erstellt werden (z. B. ein tief verschachtelter Ordnerpfad), werden andere Ereignisse als `CREATE` möglicherweise nicht ausgegeben. Um dieses Verhalten zu vermeiden, erstelle die erforderliche Ordnerstruktur im Voraus.
</Note>

<CodeGroup>
  ```js JavaScript & TypeScript icon="js" theme={"system"}
  import { Sandbox, FilesystemEventType } from 'novita-sandbox/code-interpreter'

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

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

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

  // Stop watching directory for changes
  handle.stop()

  await sandbox.kill()
  ```

  ```python Python icon="python" theme={"system"}
  from novita_sandbox.code_interpreter import Sandbox, FilesystemEventType

  sandbox = Sandbox.create()
  dirname = '/tmp'

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

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

  # Stop watching directory for changes
  handle.stop()

  sandbox.kill()
  ```
</CodeGroup>
