Handle IOError instead FileNotFound.
Refactor some parts of code.