QDirIterator#
The QDirIterator
class provides an iterator for directory entrylists. More…
Synopsis#
Functions#
Note
This documentation may contain snippets that were automatically translated from C++ to Python. We always welcome contributions to the snippet translation. If you see an issue with the translation, you can also let us know by creating a ticket on https:/bugreports.qt.io/projects/PYSIDE
Detailed Description#
Warning
This section contains snippets that were automatically translated from C++ to Python and may contain errors.
You can use 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.
The 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(): dir = it.next() print(dir) # /etc/. # /etc/.. # /etc/X11 # /etc/X11/fs # ...
Here’s how to find and read all files filtered by name, recursively:
it = QDirIterator("/sys", QStringList() << "scaling_cur_freq", QDir.NoFilter, QDirIterator.Subdirectories) while it.hasNext(): f = QFile(it.next()) f.open(QIODevice.ReadOnly) print(f.fileName(), f.readAll().trimmed().toDouble() / 1000, "MHz")
The next()
and nextFileInfo()
functions advance the iterator and return the path or the QFileInfo
of the next directory entry. You can also call filePath()
or fileInfo()
to get the current file path or QFileInfo
without first advancing the iterator. The fileName()
function returns only the name of the file, similar to how entryList()
works.
Unlike Qt’s container iterators, QDirIterator
is uni-directional (i.e., you cannot iterate directories in reverse order) and does not allow random access.
See also
- class PySide6.QtCore.QDirIterator(dir[, flags=QDirIterator.IteratorFlag.NoIteratorFlags])#
PySide6.QtCore.QDirIterator(path, filter[, flags=QDirIterator.IteratorFlag.NoIteratorFlags])
PySide6.QtCore.QDirIterator(path[, flags=QDirIterator.IteratorFlag.NoIteratorFlags])
PySide6.QtCore.QDirIterator(path, nameFilters[, filters=QDir.NoFilter[, flags=QDirIterator.IteratorFlag.NoIteratorFlags]])
- Parameters:
nameFilters – list of strings
filters – Combination of
QDir.Filter
flags – Combination of
QDirIterator.IteratorFlag
path – str
dir –
PySide6.QtCore.QDir
filter – Combination of
QDir.Filter
Constructs a 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 entryList()
.
The sorting in dir
is ignored.
Note
To list symlinks that point to non existing files, System
must be passed to the flags.
Constructs a 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 NoFilter
, and flags
is NoIteratorFlags
, which provides the same behavior as in entryList()
.
Note
To list symlinks that point to non existing files, System
must be passed to the flags.
Constructs a 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 entryList()
.
Note
To list symlinks that point to non existing files, System
must be passed to the flags.
Warning
This section contains snippets that were automatically translated from C++ to Python and may contain errors.
Constructs a 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 entryList()
.
For example, the following iterator could be used to iterate over audio files:
def audioFileIt(audioPath,{"*.mp3","*.wav"},QDir.Files):
Note
To list symlinks that point to non existing files, System
must be passed to the flags.
See also
hasNext()
next()
IteratorFlags
setNameFilters()
- PySide6.QtCore.QDirIterator.IteratorFlag#
(inherits enum.Flag
) This enum describes flags that you can combine to configure the behavior of 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.
- PySide6.QtCore.QDirIterator.fileInfo()#
- Return type:
Returns a QFileInfo
for the current directory entry.
See also
- PySide6.QtCore.QDirIterator.fileName()#
- Return type:
str
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.
See also
- PySide6.QtCore.QDirIterator.filePath()#
- Return type:
str
Returns the full file path for the current directory entry.
See also
- PySide6.QtCore.QDirIterator.hasNext()#
- Return type:
bool
Returns true
if there is at least one more entry in the directory; otherwise, false is returned.
See also
- PySide6.QtCore.QDirIterator.next()#
- Return type:
str
Advances the iterator to the next entry, and returns the file path of this new entry. If hasNext()
returns false
, this function does nothing, and returns an empty QString
.
You can call fileName()
or filePath()
to get the current entry’s file name or path, or fileInfo()
to get a QFileInfo
for the current entry.
Call nextFileInfo()
instead of next() if you’re interested in the QFileInfo
.
- PySide6.QtCore.QDirIterator.nextFileInfo()#
- Return type:
Advances the iterator to the next entry, and returns the file info of this new entry. If hasNext()
returns false
, this function does nothing, and returns an empty QFileInfo
.
You can call fileName()
or filePath()
to get the current entry’s file name or path, or fileInfo()
to get a QFileInfo
for the current entry.
Call next()
instead of nextFileInfo() when all you need is the filePath()
.
See also
- PySide6.QtCore.QDirIterator.path()#
- Return type:
str
Returns the base directory of the iterator.