Skip to content

Gio.FileMonitor

class — extends GObject.Object

Monitors a file or directory for changes.

To obtain a GFileMonitor for a file or directory, use File.monitor, File.monitor_file, or File.monitor_directory.

To get informed about changes to the file or directory you are monitoring, connect to the FileMonitor.changed signal. The signal will be emitted in the thread-default main context (see GLib.MainContext.push_thread_default) of the thread that the monitor was created in (though if the global default main context is blocked, this may cause notifications to be blocked even if the thread-default context is still running).

Methods

cancel

def cancel(self) -> bool

Cancels a file monitor.

emit_event

def emit_event(self, child: File, other_file: File | None, event_type: FileMonitorEvent | int) -> None

Emits the FileMonitor::changed signal if a change has taken place. Should be called from file monitor implementations only.

Implementations are responsible to call this method from the thread-default main context (see GLib.MainContext.push_thread_default) of the thread that the monitor was created in.

Parameters:

is_cancelled

def is_cancelled(self) -> bool

Returns whether the monitor is canceled.

set_rate_limit

def set_rate_limit(self, limit_msecs: int) -> None

Sets the rate limit to which the monitor will report consecutive change events to the same file.

Parameters:

  • limit_msecs — a non-negative integer with the limit in milliseconds to poll for changes

Virtual methods

do_cancel

def do_cancel(self) -> bool

Cancels a file monitor.

do_changed

def do_changed(self, file: File, other_file: File, event_type: FileMonitorEvent | int) -> None

Properties

cancelled

cancelled: bool  # read-only

Whether the monitor has been cancelled.

rate_limit

rate_limit: int  # read/write

The limit of the monitor to watch for changes, in milliseconds.

Signals

changed

def on_changed(self, file: File, other_file: File | None, event_type: FileMonitorEvent) -> None: ...

Emitted when file has been changed.

If using FileMonitorFlags.WATCH_MOVES on a directory monitor, and the information is available (and if supported by the backend), event_type may be FileMonitorEvent.RENAMED, FileMonitorEvent.MOVED_IN or FileMonitorEvent.MOVED_OUT.

In all cases file will be a child of the monitored directory. For renames, file will be the old name and other_file is the new name. For "moved in" events, file is the name of the file that appeared and other_file is the old name that it was moved from (in another directory). For "moved out" events, file is the name of the file that used to be in this directory and other_file is the name of the file at its new location.

It makes sense to treat FileMonitorEvent.MOVED_IN as equivalent to FileMonitorEvent.CREATED and FileMonitorEvent.MOVED_OUT as equivalent to FileMonitorEvent.DELETED, with extra information. FileMonitorEvent.RENAMED is equivalent to a delete/create pair. This is exactly how the events will be reported in the case that the FileMonitorFlags.WATCH_MOVES flag is not in use.

If using the deprecated flag FileMonitorFlags.SEND_MOVED flag and event_type is FileMonitorEvent.MOVED, file will be set to a File containing the old path, and other_file will be set to a File containing the new path.

In all the other cases, other_file will be set to None.