tidy.py: move the file matching logic to python

Performance: 30% faster (5.1s instead of 7.2s)