From ae7ea2d379ec7fb0edb8e333145bb141106a2fd2 Mon Sep 17 00:00:00 2001 From: Kyomotoi <1172294279@qq.com> Date: Sat, 20 Feb 2021 08:19:41 +0800 Subject: =?UTF-8?q?=E2=9C=A8=20=E6=9B=B4=E6=96=B0=E6=8F=92=E4=BB=B6?= =?UTF-8?q?=EF=BC=8C=E5=9F=8B=E4=B8=8Bbug?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - 更新插件: - call-owner - code-runner - status - anime-search - tex(待修复) - 埋下bug: - service中limit作为机器人服务中的开关,目前写入文件亟待修复 --- ATRI/utils/file.py | 27 +++++++++++++++++++++++++++ 1 file changed, 27 insertions(+) create mode 100644 ATRI/utils/file.py (limited to 'ATRI/utils/file.py') diff --git a/ATRI/utils/file.py b/ATRI/utils/file.py new file mode 100644 index 0000000..4699f5d --- /dev/null +++ b/ATRI/utils/file.py @@ -0,0 +1,27 @@ +import aiofiles +from pathlib import Path + +from ATRI.exceptions import WriteError + + +async def write_file(path: Path, text, encoding='utf-8') -> None: + try: + async with aiofiles.open(path, 'w', encoding=encoding) as target: + await target.write(text) + except WriteError: + raise WriteError("Writing file failed!") + + +async def open_file(path: Path, method, encoding='utf-8'): + try: + async with aiofiles.open(path, 'r', encoding=encoding) as target: + if method == "read": + return target.read() + elif method == "readlines": + return await target.readlines() + elif method == "readline": + return await target.readline() + else: + return target.readable() + except EOFError: + raise EOFError("File not fond!") -- cgit v1.2.3