Make file system actions more robust

* Instead of try/except, check if desired condition is satisfied
* PEP 8 fixes
* Python 3 compatibility fixes

Fixes: https://github.com/hashdist/hashdist/issues/286