QDirIterator

Inheritance diagram of PySide2.QtCore.QDirIterator

Synopsis

Functions

Detailed Description

The PySide2.QtCore.QDirIterator class provides an iterator for directory entrylists.

You can use PySide2.QtCore.QDirIterator to navigate entries of a directory one at a time. It is similar to QDir.entryList() and QDir.entryInfoList() , but because it lists entries one at a time instead of all at once, it scales better and is more suitable for large directories. It also supports listing directory contents recursively, and following symbolic links. Unlike QDir.entryList() , PySide2.QtCore.QDirIterator does not support sorting.

The PySide2.QtCore.QDirIterator constructor takes a PySide2.QtCore.QDir or a directory as argument. After construction, the iterator is located before the first directory entry. Here’s how to iterate over all the entries sequentially:

it = QDirIterator("/etc", QDirIterator.Subdirectories)
while it.hasNext():
    print it.next()

    # /etc/.
    # /etc/..
    # /etc/X11
    # /etc/X11/fs
    # ...

Here’s how to find and read all files filtered by name, recursively:

QDirIterator it("/sys", QStringList() << "scaling_cur_freq", QDir::NoFilter, QDirIterator::Subdirectories);
while (it.hasNext()) {
    QFile f(it.next());
    f.open(QIODevice::ReadOnly);
    qDebug() << f.fileName() << f.readAll().trimmed().toDouble() / 1000 << "MHz";
}

The PySide2.QtCore.QDirIterator.next() function returns the path to the next directory entry and advances the iterator. You can also call PySide2.QtCore.QDirIterator.filePath() to get the current file path without advancing the iterator. The PySide2.QtCore.QDirIterator.fileName() function returns only the name of the file, similar to how QDir.entryList() works. You can also call PySide2.QtCore.QDirIterator.fileInfo() to get a PySide2.QtCore.QFileInfo for the current entry.

Unlike Qt’s container iterators, PySide2.QtCore.QDirIterator is uni-directional (i.e., you cannot iterate directories in reverse order) and does not allow random access.

class PySide2.QtCore.QDirIterator(dir[, flags=QDirIterator.NoIteratorFlags])
class PySide2.QtCore.QDirIterator(path, filter[, flags=QDirIterator.NoIteratorFlags])
class PySide2.QtCore.QDirIterator(path[, flags=QDirIterator.NoIteratorFlags])
class PySide2.QtCore.QDirIterator(path, nameFilters[, filters=QDir.NoFilter[, flags=QDirIterator.NoIteratorFlags]])
Parameters:
  • path – unicode
  • filterPySide2.QtCore.QDir.Filters
  • filtersPySide2.QtCore.QDir.Filters
  • flagsPySide2.QtCore.QDirIterator.IteratorFlags
  • dirPySide2.QtCore.QDir
  • nameFilters – list of strings

Constructs a PySide2.QtCore.QDirIterator that can iterate over dir ‘s entrylist, using dir ‘s name filters and regular filters. You can pass options via flags to decide how the directory should be iterated.

By default, flags is NoIteratorFlags , which provides the same behavior as in QDir.entryList() .

The sorting in dir is ignored.

Note

To list symlinks that point to non existing files, QDir.System must be passed to the flags.

Constructs a PySide2.QtCore.QDirIterator that can iterate over path , with no name filtering and filters for entry filtering. You can pass options via flags to decide how the directory should be iterated.

By default, filters is QDir.NoFilter , and flags is NoIteratorFlags , which provides the same behavior as in QDir.entryList() .

Note

To list symlinks that point to non existing files, QDir.System must be passed to the flags.

Constructs a PySide2.QtCore.QDirIterator that can iterate over path . You can pass options via flags to decide how the directory should be iterated.

By default, flags is NoIteratorFlags , which provides the same behavior as in QDir.entryList() .

Note

To list symlinks that point to non existing files, QDir.System must be passed to the flags.

Constructs a PySide2.QtCore.QDirIterator that can iterate over path , using nameFilters and filters . You can pass options via flags to decide how the directory should be iterated.

By default, flags is NoIteratorFlags , which provides the same behavior as QDir.entryList() .

Note

To list symlinks that point to non existing files, QDir.System must be passed to the flags.

PySide2.QtCore.QDirIterator.IteratorFlag

This enum describes flags that you can combine to configure the behavior of PySide2.QtCore.QDirIterator .

Constant Description
QDirIterator.NoIteratorFlags The default value, representing no flags. The iterator will return entries for the assigned path.
QDirIterator.Subdirectories List entries inside all subdirectories as well.
QDirIterator.FollowSymlinks When combined with Subdirectories, this flag enables iterating through all subdirectories of the assigned path, following all symbolic links. Symbolic link loops (e.g., “link” => “.” or “link” => “..”) are automatically detected and ignored.
PySide2.QtCore.QDirIterator.fileInfo()
Return type:PySide2.QtCore.QFileInfo

Returns a PySide2.QtCore.QFileInfo for the current directory entry.

PySide2.QtCore.QDirIterator.fileName()
Return type:unicode

Returns the file name for the current directory entry, without the path prepended.

This function is convenient when iterating a single directory. When using the QDirIterator.Subdirectories flag, you can use PySide2.QtCore.QDirIterator.filePath() to get the full path.

PySide2.QtCore.QDirIterator.filePath()
Return type:unicode

Returns the full file path for the current directory entry.

PySide2.QtCore.QDirIterator.hasNext()
Return type:PySide2.QtCore.bool

Returns true if there is at least one more entry in the directory; otherwise, false is returned.

PySide2.QtCore.QDirIterator.next()
Return type:unicode

Advances the iterator to the next entry, and returns the file path of this new entry. If PySide2.QtCore.QDirIterator.hasNext() returns false , this function does nothing, and returns an empty PySide2.QtCore.QString .

You can call PySide2.QtCore.QDirIterator.fileName() or PySide2.QtCore.QDirIterator.filePath() to get the current entry file name or path, or PySide2.QtCore.QDirIterator.fileInfo() to get a PySide2.QtCore.QFileInfo for the current entry.

PySide2.QtCore.QDirIterator.path()
Return type:unicode

Returns the base directory of the iterator.