Spaces:
Running
Running
| # backend/tasks/tools/filesystem_tools.py | |
| import os | |
| import shutil | |
| import glob | |
| async def read_file(path: str) -> str: | |
| with open(path, 'r', encoding='utf-8') as f: | |
| return f.read() | |
| async def write_file(path: str, content: str) -> bool: | |
| os.makedirs(os.path.dirname(os.path.abspath(path)), exist_ok=True) | |
| with open(path, 'w', encoding='utf-8') as f: | |
| f.write(content) | |
| return True | |
| async def list_dir(path: str) -> list[str]: | |
| if not os.path.isdir(path): | |
| return [] | |
| return os.listdir(path) | |
| async def delete_file(path: str) -> bool: | |
| if os.path.isfile(path): | |
| os.remove(path) | |
| return True | |
| elif os.path.isdir(path): | |
| shutil.rmtree(path) | |
| return True | |
| return False | |
| async def search_files(query: str, root: str) -> list[str]: | |
| # Using glob for simple search | |
| pattern = os.path.join(root, '**', f"*{query}*") | |
| return [p for p in glob.glob(pattern, recursive=True) if os.path.isfile(p)] | |