Ensure that parent dir for known_hosts file exists

Fixes #4153. Also made pep8 fixes.