PySide6.QtCore.QDirListing

class QDirListing

QDirListing 类为目录条目提供了一个STL风格的迭代器。More_

在版本6.8中添加。

概要

方法

注意

本文档可能包含从C++自动翻译到Python的代码片段。我们始终欢迎对代码片段翻译的贡献。如果您发现翻译问题,您也可以通过在我们的https:/bugreports.qt.io/projects/PYSIDE上创建工单来告知我们。

详细描述

警告

本节包含从C++自动翻译到Python的代码片段,可能包含错误。

你可以使用QDirListing来逐个浏览目录中的条目。它类似于entryList()entryInfoList(),但由于它逐个列出条目而不是一次性列出所有条目,因此它的扩展性更好,更适合大型目录。它还支持递归列出目录内容,并遵循符号链接。与entryList()不同,QDirListing不支持排序。

QDirListing 构造函数接受一个目录路径字符串作为参数。以下是递归遍历所有条目的方法:

ItFlag = QDirListing.IteratorFlag()
for dirEntry in QDirListing("/etc", ItFlag::Recursive):
    print(dirEntry.filePath())
    # /etc/.
    # /etc/..
    # /etc/X11
    # /etc/X11/fs
    # ...

以下是如何递归地查找并读取所有按名称过滤的常规文件:

F = QDirListing.IteratorFlag()
def dirList("/sys",QStringList{"scaling_cur_freq"},F.Recursive):
for dirEntry in dirList:
    f = QFile(dirEntry.filePath())
    if f.open(QIODevice.ReadOnly):
        print(f.fileName(), f.readAll().trimmed().toDouble() / 1000, "MHz")

以下是如何递归地仅列出常规文件:

F = QDirListing.IteratorFlag()
flags = F::FilesOnly | F::Recursive
for dirEntry in QDirListing("/etc", flags):
    # ...

以下是如何递归地列出仅常规文件和常规文件的符号链接:

F = QDirListing.IteratorFlag()
flags = F::FilesOnly | F::Recursive | F::ResolveSymlinks
for dirEntry in QDirListing("/etc", flags):
    # ...

const_iterator 模拟了 C++20 的 std::input_iterator,也就是说,它是一个只能移动、只能向前、只能单次遍历的迭代器,不允许随机访问。它可以在范围 for 循环中使用(或者与不需要随机访问迭代器的 C++20 范围算法一起使用)。解引用一个有效的迭代器会返回一个 DirEntry 对象。(c) end() 哨兵标记了迭代的结束。解引用一个等于 sentinel 的迭代器是未定义行为。

DirEntry 提供了 QFileInfo 的一部分 API(例如,fileName()、filePath()、exists())。在内部,DirEntry 仅在需要时构造一个 QFileInfo 对象,也就是说,如果信息尚未被其他系统函数获取。你可以使用 fileInfo() 来获取一个 QFileInfo。例如:

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 */:

另请参阅

QDir entryList()

class IteratorFlag

(继承自 enum.Flag) 这个枚举类描述了可以用来配置 QDirListing 行为的标志。来自此枚举器的值可以进行按位或操作。

  • 常量

  • 描述

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

构建一个可以遍历pathQDirListing

您可以通过flags传递选项来控制目录的迭代方式。

默认情况下,flagsDefault

另请参阅

IteratorFlags

__init__(path, nameFilters[, flags=QDirListing.IteratorFlag.Default])
Parameters:
  • path – 字符串

  • nameFilters – 字符串列表

  • flagsIteratorFlag 的组合

警告

本节包含从C++自动翻译到Python的代码片段,可能包含错误。

构建一个可以遍历pathQDirListing

您可以通过flags传递选项来控制目录的迭代方式。默认情况下,flagsDefault

列出的条目将根据nameFilters中的文件通配符模式进行过滤(更多详情请参见setNameFilters())。

例如,以下迭代器可用于迭代音频文件:

QDirListing audioFileIt("/home/johndoe/", QStringList{"*.mp3", "*.wav"},
                        QDirListing.IteratorFlag.FilesOnly)

另请参阅

IteratorFlags setNameFilters()

__iter__()
Return type:

对象

iteratorFlags()
Return type:

IteratorFlag的组合

返回用于构造此QDirListingIteratorFlags集合。

iteratorPath()
Return type:

字符串

返回用于构造此QDirListing的目录路径。

nameFilters()
Return type:

字符串列表

返回用于构造此QDirListing的文件名通配符过滤器列表。

swap(other)
Parameters:

其他QDirListing

class DirEntry

概要

方法

注意

本文档可能包含从C++自动翻译到Python的代码片段。我们始终欢迎对代码片段翻译的贡献。如果您发现翻译问题,您也可以通过在我们的https:/bugreports.qt.io/projects/PYSIDE上创建工单来告知我们。

详细描述

警告

本节包含从C++自动翻译到Python的代码片段,可能包含错误。

解引用一个有效的 const_iterator 返回一个 DirEntry 对象。

DirEntry 提供了 QFileInfo 的 API 的一个子集(例如,fileName()filePath()exists())。在内部,DirEntry 仅在需要时构造一个 QFileInfo 对象,也就是说,如果信息尚未被其他系统函数获取。你可以使用 fileInfo() 来获取一个 QFileInfo。例如:

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:

对象

absoluteFilePath()
Return type:

字符串

absolutePath()
Return type:

字符串

baseName()
Return type:

字符串

birthTime(tz)
Parameters:

时区QTimeZone

Return type:

QDateTime

bundleName()
Return type:

字符串

canonicalFilePath()
Return type:

字符串

completeBaseName()
Return type:

字符串

completeSuffix()
Return type:

字符串

exists()
Return type:

布尔

fileInfo()
Return type:

QFileInfo

fileName()
Return type:

字符串

filePath()
Return type:

字符串

fileTime(type, tz)
Parameters:
Return type:

QDateTime

isDir()
Return type:

布尔

isExecutable()
Return type:

布尔

isFile()
Return type:

布尔

isHidden()
Return type:

布尔

isReadable()
Return type:

布尔

Return type:

布尔

isWritable()
Return type:

布尔

lastModified(tz)
Parameters:

时区QTimeZone

Return type:

QDateTime

lastRead(tz)
Parameters:

时区QTimeZone

Return type:

QDateTime

metadataChangeTime(tz)
Parameters:

时区QTimeZone

Return type:

QDateTime

size()
Return type:

整数

suffix()
Return type:

字符串