Make file system actions more robust

* Modify try/except to check if desired condition is satisfied
* PEP 8 fixes
* Python 3 compatibility fixes
Merge pull request #287 from hashdist/ahmadia/update_fileutils

Make file system actions more robust