PySide6.QtCore.QDirListing¶
- class QDirListing¶
The
QDirListingclass provides an STL-style iterator for directory entries.Details
Warning
This section contains snippets that were automatically translated from C++ to Python and may contain errors.
You can use
QDirListingto 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(),QDirListingdoes not support sorting.The
QDirListingconstructor takes a directory path string as argument. Here’s how to iterate over all entries recursively:ItFlag = QDirListing.IteratorFlag() for dirEntry in QDirListing("/etc", ItFlag::Recursive): print(dirEntry.filePath()) # /etc/. # /etc/.. # /etc/X11 # /etc/X11/fs # ...
Here’s how to find and read all regular files filtered by name, recursively:
F = QDirListing.IteratorFlag() def dirList("/sys",QStringList{"scaling_cur_freq"},F.Recursive): for dirEntry in dirList: f = QFile(dirEntry.filePath()) if f.open(QIODevice.OpenModeFlag.ReadOnly): print(f.fileName(), f.readAll().trimmed().toDouble() / 1000, "MHz")
Here’s how to list only regular files, recursively:
F = QDirListing.IteratorFlag() flags = F::FilesOnly | F::Recursive for dirEntry in QDirListing("/etc", flags): # ...
Here’s how to list only regular files and symbolic links to regular files, recursively:
F = QDirListing.IteratorFlag() flags = F::FilesOnly | F::Recursive | F::ResolveSymlinks for dirEntry in QDirListing("/etc", flags): # ...
const_iteratormodels C++20 std::input_iterator , that is, it is a move-only, forward-only, single-pass iterator, that doesn’t allow random access. It can be used in ranged-for loops (or with C++20 range algorithms that don’t require random access iterators). Dereferencing a valid iterator returns aDirEntryobject. The (c)end()sentinel marks the end of the iteration. Dereferencing an iterator that is equal tosentinelis undefined behavior.DirEntryoffers a subset ofQFileInfo‘s API (for example, fileName(), filePath(), exists()). Internally,DirEntryonly constructs aQFileInfoobject if needed, that is, if the info hasn’t been already fetched by other system functions. You can usefileInfo()to get aQFileInfo. For example:ItFlag = QDirListing.IteratorFlag() for dirEntry in QDirListing("/etc", ItFlag::Recursive): # Faster if dirEntry.fileName().endsWith(u".conf"): # This works, but might be potentially slower, since it has to construct a # QFileInfo, whereas (depending on the implementation) the fileName could # be known already if dirEntry.fileInfo().fileName().endsWith(u".conf"): ItFlag = QDirListing.IteratorFlag() for dirEntry in QDirListing("/etc", ItFlag::Recursive): # Both approaches are the same, because DirEntry will have to construct # a QFileInfo to get this info (for example, by calling system stat()) if dirEntry.size() >= 4'000 /* 4KB */: if dirEntry.fileInfo().size() >= 4'000 /* 4KB */:
See also
Added in version 6.8.
Synopsis¶
Methods¶
def
__init__()def
__iter__()def
iteratorFlags()def
iteratorPath()def
nameFilters()def
swap()
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 class describes flags that can be used to configure the behavior ofQDirListing. Values from this enumerator can be bitwise OR’ed together.Constant
Description
- __init__(path[, flags=QDirListing.IteratorFlag.Default])¶
- Parameters:
path – str
flags – Combination of
IteratorFlag
Constructs a
QDirListingthat can iterate overpath.You can pass options via
flagsto control how the directory should be iterated.By default,
flagsisDefault.See also
IteratorFlags- __init__(path, nameFilters[, flags=QDirListing.IteratorFlag.Default])
- Parameters:
path – str
nameFilters – list of strings
flags – Combination of
IteratorFlag
Warning
This section contains snippets that were automatically translated from C++ to Python and may contain errors.
Constructs a
QDirListingthat can iterate overpath.You can pass options via
flagsto control how the directory should be iterated. By default,flagsisDefault.The listed entries will be filtered according to the file glob patterns in
nameFilters, which are converted to a regular expression usingfromWildcard(seesetNameFilters()for more details).For example, the following iterator could be used to iterate over audio files:
QDirListing audioFileIt("/home/johndoe/", QStringList{"*.mp3", "*.wav"}, QDirListing.IteratorFlag.FilesOnly)
Sometimes you can filter by name more efficiently by iterating over the entries with a range-for loop, using string comparison. For example:
F = QDirListing.IteratorFlag() flags = F::FilesOnly | F::Recursive | F::ResolveSymlinks for dirEntry in QDirListing("/usr", flags): # Faster than using name filters, filter ".txt" and ".html" files # using QString API fileName = dirEntry.fileName() if fileName.endsWith(".txt") or fileName.endsWith(".html"): # ...
See also
IteratorFlagssetNameFilters()- __iter__()¶
- Return type:
object
- iteratorFlags()¶
- Return type:
Combination of
IteratorFlag
Returns the set of
IteratorFlagsused to construct thisQDirListing.- iteratorPath()¶
- Return type:
str
Returns the directory path used to construct this
QDirListing.- nameFilters()¶
- Return type:
list of strings
Returns the list of file name glob filters used to construct this
QDirListing.- swap(other)¶
- Parameters:
other –
QDirListing
- class DirEntry¶
Synopsis¶
Methods¶
def
__repr__()def
absolutePath()def
baseName()def
birthTime()def
bundleName()def
completeSuffix()def
exists()def
fileInfo()def
fileName()def
filePath()def
fileTime()def
isDir()def
isExecutable()def
isFile()def
isHidden()def
isReadable()def
isSymLink()def
isWritable()def
lastModified()def
lastRead()def
size()def
suffix()
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.
Dereferencing a valid
const_iteratorreturns aDirEntryobject.DirEntryoffers a subset ofQFileInfo‘s API (for example,fileName(),filePath(),exists()). Internally,DirEntryonly constructs aQFileInfoobject if needed, that is, if the info hasn’t been already fetched by other system functions. You can usefileInfo()to get aQFileInfo. For example:ItFlag = QDirListing.IteratorFlag() for dirEntry in QDirListing("/etc", ItFlag::Recursive): # Faster if dirEntry.fileName().endsWith(u".conf"): # This works, but might be potentially slower, since it has to construct a # QFileInfo, whereas (depending on the implementation) the fileName could # be known already if dirEntry.fileInfo().fileName().endsWith(u".conf"): ItFlag = QDirListing.IteratorFlag() for dirEntry in QDirListing("/etc", ItFlag::Recursive): # Both approaches are the same, because DirEntry will have to construct # a QFileInfo to get this info (for example, by calling system stat()) if dirEntry.size() >= 4'000 /* 4KB */: if dirEntry.fileInfo().size() >= 4'000 /* 4KB */:
- __repr__()¶
- Return type:
object
- absoluteFilePath()¶
- Return type:
str
- absolutePath()¶
- Return type:
str
- baseName()¶
- Return type:
str
- bundleName()¶
- Return type:
str
- canonicalFilePath()¶
- Return type:
str
- completeBaseName()¶
- Return type:
str
- completeSuffix()¶
- Return type:
str
- exists()¶
- Return type:
bool
- fileName()¶
- Return type:
str
- filePath()¶
- Return type:
str
- isDir()¶
- Return type:
bool
- isExecutable()¶
- Return type:
bool
- isFile()¶
- Return type:
bool
- isHidden()¶
- Return type:
bool
- isReadable()¶
- Return type:
bool
- isSymLink()¶
- Return type:
bool
- isWritable()¶
- Return type:
bool
- size()¶
- Return type:
int
- suffix()¶
- Return type:
str