contextlib.closing(object) 创建上下文管理器,在执行过程离开with语句体时自动执行object.close()。 with语句返回的值与object相同 from contextlib import closing with closing(requests.get('http://httpbin.org/get', stream=True)) as r: # Do things with the response here.