diff --git a/metagpt/utils/file.py b/metagpt/utils/file.py index 738b5a049..39ff74c32 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 = list() 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}") 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 +