一.使用with进行文件的处理
with open("Delta.txt", 'w', encoding='utf-8') as fp:
fp.write("人生苦短,我选Python")
二.使用with实现异常的捕获
class Testwith():
def __enter__(self):
print("run")
def __exit__(self, exc_type, exc_val, exc_tb):
if exc_tb is None:
print("正常结束")
else:
print(f"has error {exc_tb}")
with Testwith():
print("Test is runing")
raise NameError("testNameError")