Search code examples
pythoniteratorpysideqdir

How to use nameFilters with QDirIterator?


In PySide, when I use QDirIterator, how I can filter on files by name?

In the documentation, it talks about the parameter nameFilters:

But when I try it, it doesn't filter the files by extension:

from PySide import QtCore

it = QtCore.QDirIterator('.', nameFilters=['*.py'])
while it.hasNext():
    print it.next()

>> ./.
>> ./..
>> my_script.py
>> another_file.txt

With this code, I expected to get only the files with the extension .py.


Solution

  • The nameFilters parameter is not a keyword argument.

    Unfortunately, PySide never raises an error if you pass keyword arguments that don't exist, which is a very poor design. APIs should never fail silently when given invalid inputs.

    Anyway, your code will work correctly if you use a positional argument:

    it = QtCore.QDirIterator('.', ['*.py'])