PyQt中的os.walk类似物 [英] os.walk analogue in PyQt
问题描述
在我可以继续对某些任务进行一些过滤的递归目录/文件搜索之前,我想知道Qt/PyQt是否具有os.walk
的类似物.
Before I can continue to implement recursive dir/file search with some filtering for some tasks I want to know if Qt/PyQt has analogue of os.walk
.
主应用程序是PyQt4中的GUI应用程序,QString
s和路径对象(文件,目录)中的所有文本字段均使用QFile
,QDir
,QFileinfo
进行操作.
Main app is a GUI app in PyQt4 and all text fields in a QString
s and path objects (files, directories) uses QFile
, QDir
, QFileinfo
for manipulations.
作为模拟,我的意思是快速方便的递归fs树遍历工具.
As analogue I mean fast and convenient recursive fs-tree traversal tool.
我应该使用os.walk
还是更快,更有用的东西?
Should I use os.walk
or something much faster and more informative?
PS.也许这可以帮助我,但是我不确定这是否比os.walk
更有效.
PS. Maybe this can help me but I'm not sure if this more efficient than os.walk
.
推荐答案
我应该使用os.walk还是更快,更有用的工具?
Should I use os.walk or something much faster and more informative?
没有任何东西,如果可以的话,我建议在python中使用os.walk
.一样好.
There is none, and I would recommend using os.walk
in python if you can. It is just as good as it gets.
这不仅是因为Qt没有这种方便的方法,而且即使您基于QDir
编写自己的机制,也可以像使用os.walk
一样手工制作所有三个变量.
It is not only because Qt does not have such a convenience method, but even if you write your own mechanism based on QDir
, you will have access to all the three variables without hand-crafting like with os.walk
.
如果您迫切希望使用Qt,则可以在下面我曾经用过的下面提供以下遍历功能.
If you are desperate about using Qt, then you could have the following traverse function below I used myself a while ago.
#include <QDir>
#include <QFileInfoList>
#include <QDebug>
void traverse( const QString& dirname )
{
QDir dir(dirname);
dir.setFilter(QDir::Dirs | QDir::Files | QDir::NoSymLinks | QDir::NoDot | QDir::NoDotDot);
foreach (QFileInfo fileInfo, dir.entryInfoList()) {
if (fileInfo.isDir() && fileInfo.isReadable())
traverse(fileInfo.absoluteFilePath());
else
qDebug() << fileInfo.absoluteFilePath();
}
}
int main()
{
traverse("/usr/lib");
return 0;
}
或对于大型目录仅使用以下内容,通常来说,因为它可以更好地扩展并且更加方便:
or simply the following forfor large directories and in general since it scales better and more convenient:
#include <QDirIterator>
#include <QDebug>
int main()
{
QDirIterator it("/etc", QDirIterator::Subdirectories);
while (it.hasNext())
qDebug() << it.next();
return 0;
}
main.pro
TEMPLATE = app
TARGET = qdir-traverse
QT = core
SOURCES += main.cpp
构建并运行
qmake && make && ./qdir-traverse
然后,您将打印出所有遍历的文件.您可以开始对其进行自定义,然后进一步满足您的需求.
Then, you will get all the traversed files printed. You can start customizing it then further to your needs.
这篇关于PyQt中的os.walk类似物的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!