内容表

上一话题

QDir

下一话题

QDynamicPropertyChangeEvent

QDirIterator

QDirIterator class provides an iterator for directory entrylists. 更多

Inheritance diagram of PySide2.QtCore.QDirIterator

概要

函数

详细描述

可以使用 QDirIterator to navigate entries of a directory one at a time. It is similar to entryList() and 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 entryList() , QDirIterator does not support sorting.

QDirIterator constructor takes a 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";
}
											

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

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

class QDirIterator ( dir [ , flags=QDirIterator.NoIteratorFlags ] )

QDirIterator(path, filter[, flags=QDirIterator.NoIteratorFlags])

QDirIterator(path[, flags=QDirIterator.NoIteratorFlags])

QDirIterator(path, nameFilters[, filters=QDir.NoFilter[, flags=QDirIterator.NoIteratorFlags]])

param filter

过滤器

param dir

QDir

param path

unicode

param nameFilters

字符串列表

param filters

过滤器

param flags

IteratorFlags

构造 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.

默认情况下, filters is NoFilter ,和 flags is NoIteratorFlags , which provides the same behavior as in entryList() .

注意

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

另请参阅

hasNext() next() IteratorFlags

构造 QDirIterator that can iterate over path ,使用 nameFilters and filters . You can pass options via flags to decide how the directory should be iterated.

默认情况下, flags is NoIteratorFlags , which provides the same behavior as entryList() .

注意

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

另请参阅

hasNext() next() IteratorFlags

PySide2.QtCore.QDirIterator. IteratorFlag

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

常量

描述

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 ( )
返回类型

QFileInfo

返回 QFileInfo for the current directory entry.

PySide2.QtCore.QDirIterator. fileName ( )
返回类型

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 Subdirectories flag, you can use filePath() to get the full path.

PySide2.QtCore.QDirIterator. filePath ( )
返回类型

unicode

Returns the full file path for the current directory entry.

PySide2.QtCore.QDirIterator. hasNext ( )
返回类型

bool

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

PySide2.QtCore.QDirIterator. next ( )
返回类型

unicode

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

可以调用 fileName() or filePath() to get the current entry file name or path, or fileInfo() to get a QFileInfo for the current entry.

PySide2.QtCore.QDirIterator. path ( )
返回类型

unicode

Returns the base directory of the iterator.