fix of last commit: write to file inside try/except