Files¶
The Files service watches files and folders for changes with FSEvents. Access it via app.files.
def on_change(changes):
for change in changes:
if "created" in change.kinds:
print("new:", change.path)
watch = app.files.watch("~/Downloads", on_change)
...
watch.stop()
Methods¶
watch(path, callback, recursive=True, latency=0.3)¶
Call callback with the changes under a file or folder, and return a FileWatch.
| Parameter | Type | Description |
|---|---|---|
path |
str \| PathLike |
The file or folder; it must exist |
callback |
Callable[[list[FileChange]], None] |
Receives the changes. It runs on the event thread like a button action, so it may update views |
recursive |
bool |
Also report changes in subfolders. With False only the folder's own entries |
latency |
float |
Seconds the system waits to group changes before reporting them |
FileWatch.stop() ends the watch; calling it twice is safe. Watches also end when the app's Python process does, including on a hot reload.
FileChange¶
| Field | Type | Description |
|---|---|---|
path |
str |
The file or folder that changed |
kinds |
list[str] |
Any of "created", "removed", "renamed", "modified". A rename reports the old and the new path, each with "renamed" |
is_directory |
bool |
Whether the path is a folder |
Related¶
- Spotlight: find files instead of waiting for them
- FilePicker: let the user choose the folder to watch