If there is an exception type given to the __exit__ method, I think it would be sane to rollback the file write in the case of a write mode

    if not type:
        self.close()

Should do the trick