Simplify the handling of unrecognized units following suggestion by @mdboom
Use context manager to open file