1. 程式人生 > >上下文管理my_open

上下文管理my_open

text try readline fin Coding 上下 utf con mode

import queue
import contextlib
q=queue.Queue()
li=[]
@contextlib.contextmanager
def my_open(file_path,val):
f=open(file_path,mode=val,encoding=‘utf-8‘)
try:
yield f
finally:
f.close()
with my_open(‘ag.txt‘,‘w‘) as f1:
print(f1.readline)

上下文管理my_open