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] 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}")