Make file system actions more robust

* Modify try/except to check if desired condition is satisfied
* PEP 8 fixes
* Python 3 compatibility fixes