3

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.

Main app is a GUI app in PyQt4 and all text fields in a QStrings and path objects (files, directories) uses QFile, QDir, QFileinfo for manipulations.

As analogue I mean fast and convenient recursive fs-tree traversal tool.

Should I use os.walk or something much faster and more informative?

PS. Maybe this can help me but I'm not sure if this more efficient than os.walk.

Community
  • 1
  • 1
Крайст
  • 776
  • 1
  • 9
  • 22

1 Answers1

3

Should I use os.walk or something much faster and more informative?

There is none, and I would recommend using os.walk in python if you can. It is just as good as it gets.

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.

If you are desperate about using Qt, then you could have the following traverse function below I used myself a while ago.

main.cpp

#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

Build and Run

qmake && make && ./qdir-traverse

Then, you will get all the traversed files printed. You can start customizing it then further to your needs.

László Papp
  • 51,870
  • 39
  • 111
  • 135
  • Thank you for clear and detailed answer. Now the tool is selected and all doubts are gone. – Крайст Feb 24 '14 at 07:15
  • Hmm... So what about http://pyqt.sourceforge.net/Docs/PyQt4/qfilesystemmodel.html? – Крайст Feb 24 '14 at 07:19
  • 1
    This can be done much more efficiently with [QDirIterator](http://qt-project.org/doc/qt-4.8/qdiriterator.html). Don't know whether it would out-perform `os.walk`, though. – ekhumoro Feb 24 '14 at 07:33
  • 1
    @ekhumoro: absolutely, and I was actually writing that. The former code is from pre-qdiriterator times. – László Papp Feb 24 '14 at 07:38
  • @Laszlo: Now I'm confused :( But I will compare both of methods for speed and usecases. Thank you anyway! :) Your help gave great task to my brain to work with! – Крайст Feb 24 '14 at 07:48
  • @Крайст: why so? I would use os.walk, but I provided the alternatives if you really want to use Qt. I mean, people usually use os.walk along with the three variables. If you do not need that from os.walk, just listing the directories, the qt solutions work, otherwise you need to hand-craft the three variables, so it depends on your use case, but I had the assumption that you would need the three variables, too. There is nothing like QDir::traverse or QDir::walk in that sense. :) I would not worry much about performance because if you did, you would not use python. ;-) – László Papp Feb 24 '14 at 07:51
  • @LaszloPapp: "I would not worry much about performance because if you did, you would not use python. ;-)" — great, great, great!! Great quote at all times! :) PS. Think that topic can be closed. – Крайст Feb 24 '14 at 07:58