From 6ae0a6a98b5cdceaa343b742040d64e33c8b739d Mon Sep 17 00:00:00 2001 From: Stitch-z <284618289@qq.com> Date: Wed, 20 Sep 2023 14:05:48 +0800 Subject: [PATCH 1/3] Update: optimize universal file read ability. --- metagpt/utils/file.py | 9 ++++----- 1 file changed, 4 insertions(+), 5 deletions(-) diff --git a/metagpt/utils/file.py b/metagpt/utils/file.py index 738b5a049..50cc69cc7 100644 --- a/metagpt/utils/file.py +++ b/metagpt/utils/file.py @@ -64,17 +64,16 @@ class File: Exception: If an unexpected error occurs during the file reading process. """ try: - if not file_path.exists(): - raise FileNotFoundError(f"File not found, path is '{file_path}'") chunk_size = chunk_size or cls.CHUNK_SIZE async with aiofiles.open(file_path, mode="rb") as reader: - content = bytes() + chunks = [] while True: chunk = await reader.read(chunk_size) if not chunk: break - content += chunk - logger.info(f"Successfully read file, the size of file: {len(content)}") + chunks.append(chunk) + content = b''.join(chunks) + logger.info(f"Successfully read file, the path of file: {file_path}") return content except Exception as e: logger.error(f"Error reading file: {e}") From 35067ab0d19db9030bfcc3b2ae8fb898f30845f3 Mon Sep 17 00:00:00 2001 From: Stitch-z <284618289@qq.com> Date: Wed, 20 Sep 2023 14:07:00 +0800 Subject: [PATCH 2/3] Update: optimize universal file read ability. --- metagpt/utils/file.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/metagpt/utils/file.py b/metagpt/utils/file.py index 50cc69cc7..39ff74c32 100644 --- a/metagpt/utils/file.py +++ b/metagpt/utils/file.py @@ -66,7 +66,7 @@ class File: try: chunk_size = chunk_size or cls.CHUNK_SIZE async with aiofiles.open(file_path, mode="rb") as reader: - chunks = [] + chunks = list() while True: chunk = await reader.read(chunk_size) if not chunk: From 3b8b9639b5b1dd5fc31a7ff4ceeb1b48f4f2ba7a Mon Sep 17 00:00:00 2001 From: Stitch-z <284618289@qq.com> Date: Wed, 20 Sep 2023 14:09:01 +0800 Subject: [PATCH 3/3] Update: optimize universal file read ability. --- tests/metagpt/utils/test_file.py | 1 + 1 file changed, 1 insertion(+) diff --git a/tests/metagpt/utils/test_file.py b/tests/metagpt/utils/test_file.py index 2f224e558..b30e6be93 100644 --- a/tests/metagpt/utils/test_file.py +++ b/tests/metagpt/utils/test_file.py @@ -23,3 +23,4 @@ async def test_write_and_read_file(root_path: Path, filename: str, content: byte assert root_path / filename == full_file_name file_data = await File.read(full_file_name) assert file_data.decode("utf-8") == content +