:right-sidebar: True FileEnumerator =================================================================== .. currentmodule:: gi.repository.Gio .. class:: FileEnumerator(**properties: ~typing.Any) :no-contents-entry: Superclasses: :class:`~gi.repository.GObject.Object` ``GFileEnumerator`` allows you to operate on a set of :obj:`~gi.repository.Gio.File` objects, returning a :obj:`~gi.repository.Gio.FileInfo` structure for each file enumerated (e.g. :obj:`~gi.repository.Gio.File.enumerate_children` will return a ``GFileEnumerator`` for each of the children within a directory). To get the next file's information from a ``GFileEnumerator``\, use :obj:`~gi.repository.Gio.FileEnumerator.next_file` or its asynchronous version, :obj:`~gi.repository.Gio.FileEnumerator.next_files_async`\. Note that the asynchronous version will return a list of :obj:`~gi.repository.Gio.FileInfo` objects, whereas the synchronous will only return the next file in the enumerator. The ordering of returned files is unspecified for non-Unix platforms; for more information, see :obj:`~gi.repository.GLib.Dir.read_name`\. On Unix, when operating on local files, returned files will be sorted by inode number. Effectively you can assume that the ordering of returned files will be stable between successive calls (and applications) assuming the directory is unchanged. If your application needs a specific ordering, such as by name or modification time, you will have to implement that in your application code. To close a ``GFileEnumerator``\, use :obj:`~gi.repository.Gio.FileEnumerator.close`\, or its asynchronous version, :obj:`~gi.repository.Gio.FileEnumerator.close_async`\. Once a ``GFileEnumerator`` is closed, no further actions may be performed on it, and it should be freed with :obj:`~gi.repository.GObject.Object.unref`\. Methods ------- .. rst-class:: interim-class .. class:: FileEnumerator :no-index: .. method:: close(cancellable: ~gi.repository.Gio.Cancellable | None = None) -> bool Releases all resources used by this enumerator, making the enumerator return :const:`~gi.repository.Gio.IOErrorEnum.CLOSED` on all calls. This will be automatically called when the last reference is dropped, but you might want to call this function to make sure resources are released as early as possible. :param cancellable: optional :obj:`~gi.repository.Gio.Cancellable` object, :const:`None` to ignore. .. method:: close_async(io_priority: int, cancellable: ~gi.repository.Gio.Cancellable | None = None, callback: ~typing.Callable[[~gi.repository.GObject.Object | None, ~gi.repository.Gio.AsyncResult, ~typing.Any], None] | None = None, user_data: ~typing.Any = None) -> None Asynchronously closes the file enumerator. If ``cancellable`` is not :const:`None`, then the operation can be cancelled by triggering the cancellable object from another thread. If the operation was cancelled, the error :const:`~gi.repository.Gio.IOErrorEnum.CANCELLED` will be returned in :func:`~gi.repository.Gio.FileEnumerator.close_finish`. :param io_priority: the `I/O priority `__ of the request :param cancellable: optional :obj:`~gi.repository.Gio.Cancellable` object, :const:`None` to ignore. :param callback: a :obj:`~gi.repository.Gio.AsyncReadyCallback` to call when the request is satisfied :param user_data: the data to pass to callback function .. method:: close_finish(result: ~gi.repository.Gio.AsyncResult) -> bool Finishes closing a file enumerator, started from :func:`~gi.repository.Gio.FileEnumerator.close_async`. If the file enumerator was already closed when :func:`~gi.repository.Gio.FileEnumerator.close_async` was called, then this function will report :const:`~gi.repository.Gio.IOErrorEnum.CLOSED` in ``error``\, and return :const:`False`. If the file enumerator had pending operation when the close operation was started, then this function will report :const:`~gi.repository.Gio.IOErrorEnum.PENDING`, and return :const:`False`. If ``cancellable`` was not :const:`None`, then the operation may have been cancelled by triggering the cancellable object from another thread. If the operation was cancelled, the error :const:`~gi.repository.Gio.IOErrorEnum.CANCELLED` will be set, and :const:`False` will be returned. :param result: a :obj:`~gi.repository.Gio.AsyncResult`\. .. method:: do_close_async(self, io_priority: int, cancellable: ~gi.repository.Gio.Cancellable | None = None, callback: ~typing.Callable[[~gi.repository.GObject.Object | None, ~gi.repository.Gio.AsyncResult, ~typing.Any], None] | None = None, user_data: ~typing.Any = None) -> None :param io_priority: :param cancellable: :param callback: :param user_data: .. method:: do_close_finish(self, result: ~gi.repository.Gio.AsyncResult) -> bool :param result: .. method:: do_close_fn(self, cancellable: ~gi.repository.Gio.Cancellable | None = None) -> bool :param cancellable: .. method:: do_next_file(self, cancellable: ~gi.repository.Gio.Cancellable | None = None) -> ~gi.repository.Gio.FileInfo | None :param cancellable: .. method:: do_next_files_async(self, num_files: int, io_priority: int, cancellable: ~gi.repository.Gio.Cancellable | None = None, callback: ~typing.Callable[[~gi.repository.GObject.Object | None, ~gi.repository.Gio.AsyncResult, ~typing.Any], None] | None = None, user_data: ~typing.Any = None) -> None :param num_files: :param io_priority: :param cancellable: :param callback: :param user_data: .. method:: do_next_files_finish(self, result: ~gi.repository.Gio.AsyncResult) -> list[~gi.repository.Gio.FileInfo] :param result: .. method:: get_child(info: ~gi.repository.Gio.FileInfo) -> ~gi.repository.Gio.File Return a new :obj:`~gi.repository.Gio.File` which refers to the file named by ``info`` in the source directory of ``enumerator``\. This function is primarily intended to be used inside loops with :func:`~gi.repository.Gio.FileEnumerator.next_file`. To use this, :const:`~gi.repository.Gio.FILE_ATTRIBUTE_STANDARD_NAME` must have been listed in the attributes list used when creating the :obj:`~gi.repository.Gio.FileEnumerator`\. This is a convenience method that's equivalent to: .. code-block:: C :dedent: gchar *name = g_file_info_get_name (info); GFile *child = g_file_get_child (g_file_enumerator_get_container (enumr), name); .. versionadded:: 2.36 :param info: a :obj:`~gi.repository.Gio.FileInfo` gotten from :func:`~gi.repository.Gio.FileEnumerator.next_file` or the async equivalents. .. method:: get_container() -> ~gi.repository.Gio.File Get the :obj:`~gi.repository.Gio.File` container which is being enumerated. .. versionadded:: 2.18 .. method:: has_pending() -> bool Checks if the file enumerator has pending operations. .. method:: is_closed() -> bool Checks if the file enumerator has been closed. .. method:: iterate(cancellable: ~gi.repository.Gio.Cancellable | None = None) -> ~typing.Tuple[bool, ~gi.repository.Gio.FileInfo, ~gi.repository.Gio.File] This is a version of :func:`~gi.repository.Gio.FileEnumerator.next_file` that's easier to use correctly from C programs. With :func:`~gi.repository.Gio.FileEnumerator.next_file`, the gboolean return value signifies "end of iteration or error", which requires allocation of a temporary :obj:`~gi.repository.GLib.Error`\. In contrast, with this function, a :const:`False` return from :func:`~gi.repository.Gio.FileEnumerator.iterate` *always* means "error". End of iteration is signaled by ``out_info`` or ``out_child`` being :const:`None`. Another crucial difference is that the references for ``out_info`` and ``out_child`` are owned by ``direnum`` (they are cached as hidden properties). You must not unref them in your own code. This makes memory management significantly easier for C code in combination with loops. Finally, this function optionally allows retrieving a :obj:`~gi.repository.Gio.File` as well. You must specify at least one of ``out_info`` or ``out_child``\. The code pattern for correctly using :func:`~gi.repository.Gio.FileEnumerator.iterate` from C is: .. code-block:: :dedent: direnum = g_file_enumerate_children (file, ...); while (TRUE) { GFileInfo *info; if (!g_file_enumerator_iterate (direnum, &info, NULL, cancellable, error)) goto out; if (!info) break; ... do stuff with "info"; do not unref it! ... } out: g_object_unref (direnum); // Note: frees the last @info .. versionadded:: 2.44 :param cancellable: a :obj:`~gi.repository.Gio.Cancellable` .. method:: next_file(cancellable: ~gi.repository.Gio.Cancellable | None = None) -> ~gi.repository.Gio.FileInfo | None Returns information for the next file in the enumerated object. Will block until the information is available. The :obj:`~gi.repository.Gio.FileInfo` returned from this function will contain attributes that match the attribute string that was passed when the :obj:`~gi.repository.Gio.FileEnumerator` was created. See the documentation of :obj:`~gi.repository.Gio.FileEnumerator` for information about the order of returned files. On error, returns :const:`None` and sets ``error`` to the error. If the enumerator is at the end, :const:`None` will be returned and ``error`` will be unset. :param cancellable: optional :obj:`~gi.repository.Gio.Cancellable` object, :const:`None` to ignore. .. method:: next_files_async(num_files: int, io_priority: int, cancellable: ~gi.repository.Gio.Cancellable | None = None, callback: ~typing.Callable[[~gi.repository.GObject.Object | None, ~gi.repository.Gio.AsyncResult, ~typing.Any], None] | None = None, user_data: ~typing.Any = None) -> None Request information for a number of files from the enumerator asynchronously. When all I/O for the operation is finished the ``callback`` will be called with the requested information. See the documentation of :obj:`~gi.repository.Gio.FileEnumerator` for information about the order of returned files. Once the end of the enumerator is reached, or if an error occurs, the ``callback`` will be called with an empty list. In this case, the previous call to :func:`~gi.repository.Gio.FileEnumerator.next_files_async` will typically have returned fewer than ``num_files`` items. If a request is cancelled the callback will be called with :const:`~gi.repository.Gio.IOErrorEnum.CANCELLED`. This leads to the following pseudo-code usage: .. code-block:: :dedent: g_autoptr(GFile) dir = get_directory (); g_autoptr(GFileEnumerator) enumerator = NULL; g_autolist(GFileInfo) files = NULL; g_autoptr(GError) local_error = NULL; enumerator = yield g_file_enumerate_children_async (dir, G_FILE_ATTRIBUTE_STANDARD_NAME "," G_FILE_ATTRIBUTE_STANDARD_TYPE, G_FILE_QUERY_INFO_NONE, G_PRIORITY_DEFAULT, cancellable, …, &local_error); if (enumerator == NULL) g_error ("Error enumerating: %s", local_error->message); // Loop until no files are returned, either because the end of the enumerator // has been reached, or an error was returned. do { files = yield g_file_enumerator_next_files_async (enumerator, 5, // number of files to request G_PRIORITY_DEFAULT, cancellable, …, &local_error); // Process the returned files, but don’t assume that exactly 5 were returned. for (GList *l = files; l != NULL; l = l->next) { GFileInfo *info = l->data; handle_file_info (info); } } while (files != NULL); if (local_error != NULL && !g_error_matches (local_error, G_IO_ERROR, G_IO_ERROR_CANCELLED)) g_error ("Error while enumerating: %s", local_error->message); During an async request no other sync and async calls are allowed, and will result in :const:`~gi.repository.Gio.IOErrorEnum.PENDING` errors. Any outstanding I/O request with higher priority (lower numerical value) will be executed before an outstanding request with lower priority. Default priority is %G_PRIORITY_DEFAULT. :param num_files: the number of file info objects to request :param io_priority: the `I/O priority `__ of the request :param cancellable: optional :obj:`~gi.repository.Gio.Cancellable` object, :const:`None` to ignore. :param callback: a :obj:`~gi.repository.Gio.AsyncReadyCallback` to call when the request is satisfied :param user_data: the data to pass to callback function .. method:: next_files_finish(result: ~gi.repository.Gio.AsyncResult) -> list[~gi.repository.Gio.FileInfo] Finishes the asynchronous operation started with :func:`~gi.repository.Gio.FileEnumerator.next_files_async`. :param result: a :obj:`~gi.repository.Gio.AsyncResult`\. .. method:: set_pending(pending: bool) -> None Sets the file enumerator as having pending operations. :param pending: a boolean value. Properties ---------- .. rst-class:: interim-class .. class:: FileEnumerator :no-index: .. attribute:: props.container :type: ~gi.repository.Gio.File The type of the None singleton. Virtual Methods --------------- .. rst-class:: interim-class .. class:: FileEnumerator :no-index: .. method:: do_close_async(io_priority: int, cancellable: ~gi.repository.Gio.Cancellable | None = None, callback: ~typing.Callable[[~gi.repository.GObject.Object | None, ~gi.repository.Gio.AsyncResult, ~typing.Any], None] | None = None, user_data: ~typing.Any = None) -> None Asynchronously closes the file enumerator. If ``cancellable`` is not :const:`None`, then the operation can be cancelled by triggering the cancellable object from another thread. If the operation was cancelled, the error :const:`~gi.repository.Gio.IOErrorEnum.CANCELLED` will be returned in :func:`~gi.repository.Gio.FileEnumerator.close_finish`. :param io_priority: the `I/O priority `__ of the request :param cancellable: optional :obj:`~gi.repository.Gio.Cancellable` object, :const:`None` to ignore. :param callback: a :obj:`~gi.repository.Gio.AsyncReadyCallback` to call when the request is satisfied :param user_data: the data to pass to callback function .. method:: do_close_finish(result: ~gi.repository.Gio.AsyncResult) -> bool Finishes closing a file enumerator, started from :func:`~gi.repository.Gio.FileEnumerator.close_async`. If the file enumerator was already closed when :func:`~gi.repository.Gio.FileEnumerator.close_async` was called, then this function will report :const:`~gi.repository.Gio.IOErrorEnum.CLOSED` in ``error``\, and return :const:`False`. If the file enumerator had pending operation when the close operation was started, then this function will report :const:`~gi.repository.Gio.IOErrorEnum.PENDING`, and return :const:`False`. If ``cancellable`` was not :const:`None`, then the operation may have been cancelled by triggering the cancellable object from another thread. If the operation was cancelled, the error :const:`~gi.repository.Gio.IOErrorEnum.CANCELLED` will be set, and :const:`False` will be returned. :param result: a :obj:`~gi.repository.Gio.AsyncResult`\. .. method:: do_close_fn(cancellable: ~gi.repository.Gio.Cancellable | None = None) -> bool The type of the None singleton. :param cancellable: .. method:: do_next_file(cancellable: ~gi.repository.Gio.Cancellable | None = None) -> ~gi.repository.Gio.FileInfo | None Returns information for the next file in the enumerated object. Will block until the information is available. The :obj:`~gi.repository.Gio.FileInfo` returned from this function will contain attributes that match the attribute string that was passed when the :obj:`~gi.repository.Gio.FileEnumerator` was created. See the documentation of :obj:`~gi.repository.Gio.FileEnumerator` for information about the order of returned files. On error, returns :const:`None` and sets ``error`` to the error. If the enumerator is at the end, :const:`None` will be returned and ``error`` will be unset. :param cancellable: optional :obj:`~gi.repository.Gio.Cancellable` object, :const:`None` to ignore. .. method:: do_next_files_async(num_files: int, io_priority: int, cancellable: ~gi.repository.Gio.Cancellable | None = None, callback: ~typing.Callable[[~gi.repository.GObject.Object | None, ~gi.repository.Gio.AsyncResult, ~typing.Any], None] | None = None, user_data: ~typing.Any = None) -> None Request information for a number of files from the enumerator asynchronously. When all I/O for the operation is finished the ``callback`` will be called with the requested information. See the documentation of :obj:`~gi.repository.Gio.FileEnumerator` for information about the order of returned files. Once the end of the enumerator is reached, or if an error occurs, the ``callback`` will be called with an empty list. In this case, the previous call to :func:`~gi.repository.Gio.FileEnumerator.next_files_async` will typically have returned fewer than ``num_files`` items. If a request is cancelled the callback will be called with :const:`~gi.repository.Gio.IOErrorEnum.CANCELLED`. This leads to the following pseudo-code usage: .. code-block:: :dedent: g_autoptr(GFile) dir = get_directory (); g_autoptr(GFileEnumerator) enumerator = NULL; g_autolist(GFileInfo) files = NULL; g_autoptr(GError) local_error = NULL; enumerator = yield g_file_enumerate_children_async (dir, G_FILE_ATTRIBUTE_STANDARD_NAME "," G_FILE_ATTRIBUTE_STANDARD_TYPE, G_FILE_QUERY_INFO_NONE, G_PRIORITY_DEFAULT, cancellable, …, &local_error); if (enumerator == NULL) g_error ("Error enumerating: %s", local_error->message); // Loop until no files are returned, either because the end of the enumerator // has been reached, or an error was returned. do { files = yield g_file_enumerator_next_files_async (enumerator, 5, // number of files to request G_PRIORITY_DEFAULT, cancellable, …, &local_error); // Process the returned files, but don’t assume that exactly 5 were returned. for (GList *l = files; l != NULL; l = l->next) { GFileInfo *info = l->data; handle_file_info (info); } } while (files != NULL); if (local_error != NULL && !g_error_matches (local_error, G_IO_ERROR, G_IO_ERROR_CANCELLED)) g_error ("Error while enumerating: %s", local_error->message); During an async request no other sync and async calls are allowed, and will result in :const:`~gi.repository.Gio.IOErrorEnum.PENDING` errors. Any outstanding I/O request with higher priority (lower numerical value) will be executed before an outstanding request with lower priority. Default priority is %G_PRIORITY_DEFAULT. :param num_files: the number of file info objects to request :param io_priority: the `I/O priority `__ of the request :param cancellable: optional :obj:`~gi.repository.Gio.Cancellable` object, :const:`None` to ignore. :param callback: a :obj:`~gi.repository.Gio.AsyncReadyCallback` to call when the request is satisfied :param user_data: the data to pass to callback function .. method:: do_next_files_finish(result: ~gi.repository.Gio.AsyncResult) -> list[~gi.repository.Gio.FileInfo] Finishes the asynchronous operation started with :func:`~gi.repository.Gio.FileEnumerator.next_files_async`. :param result: a :obj:`~gi.repository.Gio.AsyncResult`\. Fields ------ .. rst-class:: interim-class .. class:: FileEnumerator :no-index: .. attribute:: parent_instance .. attribute:: priv