PySide6.QtCore.QDirIterator¶
- class QDirIterator¶
The
QDirIteratorclass provides an iterator for directory entrylists.Details
Warning
This section contains snippets that were automatically translated from C++ to Python and may contain errors.
You can use
QDirIteratorto navigate entries of a directory one at a time. It is similar toentryList()andentryInfoList(), 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. UnlikeentryList(),QDirIteratordoes not support sorting.The
QDirIteratorconstructor takes aQDiror 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.OpenModeFlag.ReadOnly) print(f.fileName(), f.readAll().trimmed().toDouble() / 1000, "MHz")
The
next()andnextFileInfo()functions advance the iterator and return the path or theQFileInfoof the next directory entry. You can also callfilePath()orfileInfo()to get the current file path orQFileInfowithout first advancing the iterator. ThefileName()function returns only the name of the file, similar to howentryList()works.Unlike Qt’s container iterators,
QDirIteratoris uni-directional (i.e., you cannot iterate directories in reverse order) and does not allow random access.Note
This class is deprecated and may be removed in a Qt release. Use
QDirListinginstead, see Porting QDirIterator to QDirListing .See also
Synopsis¶
Methods¶
def
__init__()def
fileInfo()def
fileName()def
filePath()def
hasNext()def
next()def
nextFileInfo()def
path()
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
- class IteratorFlag¶
(inherits
enum.Flag) This enum describes flags that you can combine to configure the behavior ofQDirIterator.Constant
Description
QDirIterator.IteratorFlag.NoIteratorFlags
The default value, representing no flags. The iterator will return entries for the assigned path.
QDirIterator.IteratorFlag.Subdirectories
List entries inside all subdirectories as well.
QDirIterator.IteratorFlag.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.
- __init__(dir[, flags=QDirIterator.IteratorFlag.NoIteratorFlags])¶
- Parameters:
dir –
QDirflags – Combination of
IteratorFlag
Constructs a
QDirIteratorthat can iterate overdir's entrylist, usingdir's name filters and regular filters. You can pass options viaflagsto decide how the directory should be iterated.By default,
flagsisNoIteratorFlags, which provides the same behavior as inentryList().The sorting in
diris ignored.Note
To list symlinks that point to non existing files,
Systemmust be passed to the flags.- __init__(path[, flags=QDirIterator.IteratorFlag.NoIteratorFlags])
- Parameters:
path – str
flags – Combination of
IteratorFlag
Constructs a
QDirIteratorthat can iterate overpath. You can pass options viaflagsto decide how the directory should be iterated.By default,
flagsisNoIteratorFlags, which provides the same behavior as inentryList().Note
To list symlinks that point to non existing files,
Systemmust be passed to the flags.- __init__(path, filter[, flags=QDirIterator.IteratorFlag.NoIteratorFlags])
- Parameters:
path – str
filter – Combination of
Filterflags – Combination of
IteratorFlag
Constructs a
QDirIteratorthat can iterate overpath, with no name filtering andfiltersfor entry filtering. You can pass options viaflagsto decide how the directory should be iterated.By default,
filtersisNoFilter, andflagsisNoIteratorFlags, which provides the same behavior as inentryList().Note
To list symlinks that point to non existing files,
Systemmust be passed to the flags.- __init__(path, nameFilters[, filters=QDir.NoFilter[, flags=QDirIterator.IteratorFlag.NoIteratorFlags]])
- Parameters:
path – str
nameFilters – list of strings
filters – Combination of
Filterflags – Combination of
IteratorFlag
Warning
This section contains snippets that were automatically translated from C++ to Python and may contain errors.
Constructs a
QDirIteratorthat can iterate overpath, usingnameFiltersandfilters. You can pass options viaflagsto decide how the directory should be iterated.By default,
flagsisNoIteratorFlags, which provides the same behavior asentryList().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,
Systemmust be passed to the flags.See also
hasNext()next()IteratorFlagssetNameFilters()Returns a
QFileInfofor the current directory entry.See also
- 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
Subdirectoriesflag, you can usefilePath()to get the full path.See also
- filePath()¶
- Return type:
str
Returns the full file path for the current directory entry.
See also
- hasNext()¶
- Return type:
bool
Returns
trueif there is at least one more entry in the directory; otherwise, false is returned.See also
- next()¶
- Return type:
str
Advances the iterator to the next entry, and returns the file path of this new entry. If
hasNext()returnsfalse, this function does nothing, and returns an emptyQString. Ideally you should always callhasNext()before calling this method.You can call
fileName()orfilePath()to get the current entry’s file name or path, orfileInfo()to get aQFileInfofor the current entry.Call
nextFileInfo()instead of next() if you’re interested in theQFileInfo.Advances the iterator to the next entry, and returns the file info of this new entry. If
hasNext()returnsfalse, this function does nothing, and returns an emptyQFileInfo. Ideally you should always callhasNext()before calling this method.You can call
fileName()orfilePath()to get the current entry’s file name or path, orfileInfo()to get aQFileInfofor the current entry.Call
next()instead of nextFileInfo() when all you need is thefilePath().See also
- path()¶
- Return type:
str
Returns the base directory of the iterator.