I would do something like this here https://github.com/apache/libcloud/pull/115#discussion_r4831237

Please also use `with` context manager to make sure file is always closed.