from pathlib import Path target = Path(r'C:\wechat_rpa\test_protocol_review_assistant.py') source = target.read_text(encoding='utf-8') source = source.replace(" with sqlite3.connect(self.coordinator.path) as conn:\n", " from contextlib import closing\n with closing(sqlite3.connect(self.coordinator.path)) as conn:\n") target.write_text(source, encoding='utf-8', newline='\n') print('Close test assertion SQLite connection explicitly')