PySide6.QtCore.QDirListing

class QDirListing

The QDirListing class 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 QDirListing 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() , QDirListing does not support sorting.

The QDirListing constructor 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_iterator models 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 a DirEntry object. The (c) end() sentinel marks the end of the iteration. Dereferencing an iterator that is equal to sentinel is undefined behavior.

DirEntry offers a subset of QFileInfo ‘s API (for example, fileName(), filePath(), exists()). Internally, DirEntry only constructs a QFileInfo object if needed, that is, if the info hasn’t been already fetched by other system functions. You can use fileInfo() to get a QFileInfo . 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

QDir entryList()

Added in version 6.8.

Synopsis

Methods

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 of QDirListing . Values from this enumerator can be bitwise OR’ed together.

  • Constant

  • Description

__init__(path[, flags=QDirListing.IteratorFlag.Default])
Parameters:

Constructs a QDirListing that can iterate over path.

You can pass options via flags to control how the directory should be iterated.

By default, flags is Default .

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 QDirListing that can iterate over path.

You can pass options via flags to control how the directory should be iterated. By default, flags is Default .

The listed entries will be filtered according to the file glob patterns in nameFilters, which are converted to a regular expression using fromWildcard (see setNameFilters() 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

IteratorFlags setNameFilters()

__iter__()
Return type:

object

iteratorFlags()
Return type:

Combination of IteratorFlag

Returns the set of IteratorFlags used to construct this QDirListing .

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:

otherQDirListing

class DirEntry

Synopsis

Methods

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_iterator returns a DirEntry object.

DirEntry offers a subset of QFileInfo ‘s API (for example, fileName() , filePath() , exists() ). Internally, DirEntry only constructs a QFileInfo object if needed, that is, if the info hasn’t been already fetched by other system functions. You can use fileInfo() to get a QFileInfo . 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

birthTime(tz)
Parameters:

tzQTimeZone

Return type:

QDateTime

bundleName()
Return type:

str

canonicalFilePath()
Return type:

str

completeBaseName()
Return type:

str

completeSuffix()
Return type:

str

exists()
Return type:

bool

fileInfo()
Return type:

QFileInfo

fileName()
Return type:

str

filePath()
Return type:

str

fileTime(type, tz)
Parameters:
Return type:

QDateTime

isDir()
Return type:

bool

isExecutable()
Return type:

bool

isFile()
Return type:

bool

isHidden()
Return type:

bool

isReadable()
Return type:

bool

Return type:

bool

isWritable()
Return type:

bool

lastModified(tz)
Parameters:

tzQTimeZone

Return type:

QDateTime

lastRead(tz)
Parameters:

tzQTimeZone

Return type:

QDateTime

metadataChangeTime(tz)
Parameters:

tzQTimeZone

Return type:

QDateTime

size()
Return type:

int

suffix()
Return type:

str