Skip to content

Open-source Python library for AI browser agents, plus Browser Use Cloud: stealth browsers and a hosted agent API.

Refactor file appending logic in file_system.py to use a dedicated function for writing content, improving readability and maintainability. Enhanced error handling to include exception messages for better debugging.

Magnus Müller0871e4cf2875
diff --git a/browser_use/filesystem/file_system.py b/browser_use/filesystem/file_system.py
index 99fd99906..f7a4c176c 100644
--- a/browser_use/filesystem/file_system.py
+++ b/browser_use/filesystem/file_system.py
@@ -85,10 +85,14 @@ class FileSystem:
# Create a new executor for this operation
with ThreadPoolExecutor() as executor:
# Run file append in a thread to avoid blocking
- await asyncio.get_event_loop().run_in_executor(executor, lambda p=path, c=content: p.write_text(c, append=True))
+ def append_to_file():
+ with path.open('a') as f:
+ f.write(content)
+
+ await asyncio.get_event_loop().run_in_executor(executor, append_to_file)
return f'Data appended to {file_name} successfully.'
- except Exception:
- return f"Error: Could not append to file '{file_name}'."
+ except Exception as e:
+ return f"Error: Could not append to file '{file_name}'. {str(e)}"
def describe(self) -> str:
"""List all files with their line counts."""