PySide6.QtCore.QDirListing¶
- class QDirListing¶
QDirListing类为目录条目提供了一个STL风格的迭代器。More_…在版本6.8中添加。
概要¶
方法¶
def
__init__()def
__iter__()def
iteratorFlags()def
iteratorPath()def
nameFilters()def
swap()
注意
本文档可能包含从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 */:
另请参阅
- class IteratorFlag¶
(继承自
enum.Flag) 这个枚举类描述了可以用来配置QDirListing行为的标志。来自此枚举器的值可以进行按位或操作。常量
描述
- __init__(path[, flags=QDirListing.IteratorFlag.Default])¶
- Parameters:
path – 字符串
flags –
IteratorFlag的组合
构建一个可以遍历
path的QDirListing。您可以通过
flags传递选项来控制目录的迭代方式。默认情况下,
flags是Default。另请参阅
IteratorFlags- __init__(path, nameFilters[, flags=QDirListing.IteratorFlag.Default])
- Parameters:
path – 字符串
nameFilters – 字符串列表
flags –
IteratorFlag的组合
警告
本节包含从C++自动翻译到Python的代码片段,可能包含错误。
构建一个可以遍历
path的QDirListing。您可以通过
flags传递选项来控制目录的迭代方式。默认情况下,flags是Default。列出的条目将根据
nameFilters中的文件通配符模式进行过滤(更多详情请参见setNameFilters())。例如,以下迭代器可用于迭代音频文件:
QDirListing audioFileIt("/home/johndoe/", QStringList{"*.mp3", "*.wav"}, QDirListing.IteratorFlag.FilesOnly)
另请参阅
IteratorFlagssetNameFilters()- __iter__()¶
- Return type:
对象
- iteratorFlags()¶
- Return type:
IteratorFlag的组合
返回用于构造此
QDirListing的IteratorFlags集合。- iteratorPath()¶
- Return type:
字符串
返回用于构造此
QDirListing的目录路径。- nameFilters()¶
- Return type:
字符串列表
返回用于构造此
QDirListing的文件名通配符过滤器列表。- swap(other)¶
- Parameters:
其他 –
QDirListing
- class DirEntry¶
概要¶
方法¶
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()
注意
本文档可能包含从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:
字符串
- bundleName()¶
- Return type:
字符串
- canonicalFilePath()¶
- Return type:
字符串
- completeBaseName()¶
- Return type:
字符串
- completeSuffix()¶
- Return type:
字符串
- exists()¶
- Return type:
布尔
- fileName()¶
- Return type:
字符串
- filePath()¶
- Return type:
字符串
- isDir()¶
- Return type:
布尔
- isExecutable()¶
- Return type:
布尔
- isFile()¶
- Return type:
布尔
- isHidden()¶
- Return type:
布尔
- isReadable()¶
- Return type:
布尔
- isSymLink()¶
- Return type:
布尔
- isWritable()¶
- Return type:
布尔
- size()¶
- Return type:
整数
- suffix()¶
- Return type:
字符串