seek operation in python

Chris Angelico rosuav at
Thu Apr 30 11:39:10 CEST 2015

On Thu, Apr 30, 2015 at 7:06 PM, Cecil Westerhof <Cecil at> wrote:
> I already done it. I thought it not to much work. And it even makes
> some code shorter:
> -    marshal_file    = open(expanduser(marshal_filename), 'r')
> -    not_list        = load(marshal_file)
> -    marshal_file.close()
> -    return not_list
> +    with open(expanduser(marshal_filename), 'r') as f:
> +        return load(f)
> But here I did the close myself already, so that is not completely
> honest of me. ;-)

The context manager makes your code more concise AND more reliable -
an exception thrown by load() will skip the explicit close(), but the
'with' block unwinds correctly whether there's an exception or not.


More information about the Python-list mailing list