directory.py: fail sooner (with a better error) on invalid filenames.

Thanks to Matthieu Rakotojaona.