标签:python with open python上下文 contextlib
import contextlib @contextlib.contextmanager def myopen(file, mode): f = open(file, mode, encoding="utf-8") try: yield f finally: f.close() with myopen("01-thread.py", ‘r‘) as f: print(f.read())
这里使用Python contextlib模块模拟了我们常用的with open功能,这里使用了contextlib.contextmanager装饰器,不能缺失!
本文出自 “戴柏阳的博客” 博客,请务必保留此出处http://daibaiyang119.blog.51cto.com/3145591/1964018
标签:python with open python上下文 contextlib
原文地址:http://daibaiyang119.blog.51cto.com/3145591/1964018