Skip to content

Filesystem

Filesystem endpoints let you list, read, write, move, copy, delete, and search files inside a running sandbox. File content is transferred as raw binary bytes.

List directory entries.

from leap0 import Leap0Client
client = Leap0Client()
sandbox = client.sandboxes.create()
result = sandbox.filesystem.ls(
path="/workspace",
recursive=False,
exclude=["node_modules", ".git"],
)
print(len(result.items))

Get metadata for a single path.

from leap0 import Leap0Client
client = Leap0Client()
sandbox = client.sandboxes.create()
info = sandbox.filesystem.stat(path="/workspace/main.py")
print(info.size)

Create a directory. Set recursive to create parent directories.

from leap0 import Leap0Client
client = Leap0Client()
sandbox = client.sandboxes.create()
sandbox.filesystem.mkdir(
path="/workspace/src",
recursive=True,
permissions="755",
)

Write raw binary content to a single path. The file path is passed as a query parameter and the request body is the raw file bytes.

from leap0 import Leap0Client
client = Leap0Client()
sandbox = client.sandboxes.create()
sandbox.filesystem.write_bytes(
path="/workspace/hello.txt",
content=b"Hello, world!",
permissions="644",
)

Write multiple files in a single request using multipart/form-data. Each part’s name is the file path and the body is the raw file bytes.

from leap0 import Leap0Client
client = Leap0Client()
sandbox = client.sandboxes.create()
sandbox.filesystem.write_files_bytes(
files={
"/workspace/a.txt": b"a",
"/workspace/b.txt": b"b",
}
)

Read a single file. The response body contains the raw file bytes with content type application/octet-stream.

Use head to read only the first N lines or tail to read only the last N lines. These options are mutually exclusive.

from leap0 import Leap0Client
client = Leap0Client()
sandbox = client.sandboxes.create()
print(sandbox.filesystem.read_bytes(path="/workspace/main.py"))
print(sandbox.filesystem.read_bytes(path="/workspace/main.py", head=20))
print(sandbox.filesystem.read_bytes(path="/workspace/main.py", tail=10))

Read multiple files. The response is multipart/form-data where each part’s name is the file path and the body is the raw file bytes.

from leap0 import Leap0Client
client = Leap0Client()
sandbox = client.sandboxes.create()
files = sandbox.filesystem.read_files_bytes(
paths=["/workspace/a.txt", "/workspace/b.txt"],
)
for path, content in files.items():
print(path, content)

Delete a file or directory. Set recursive for directories with contents.

from leap0 import Leap0Client
client = Leap0Client()
sandbox = client.sandboxes.create()
sandbox.filesystem.delete(path="/workspace/old", recursive=True)

Set file mode and optionally change owner and group.

from leap0 import Leap0Client
client = Leap0Client()
sandbox = client.sandboxes.create()
sandbox.filesystem.set_permissions(
path="/workspace/script.sh",
mode="755",
owner="root",
group="root",
)

Find file paths matching a glob pattern.

from leap0 import Leap0Client
client = Leap0Client()
sandbox = client.sandboxes.create()
matches = sandbox.filesystem.glob(
path="/workspace",
pattern="*.ts",
exclude=["node_modules"],
)
print(len(matches))

Search for a text pattern across files in a directory.

from leap0 import Leap0Client
client = Leap0Client()
sandbox = client.sandboxes.create()
matches = sandbox.filesystem.grep(
path="/workspace",
pattern="TODO",
include="*.py",
exclude=["node_modules", ".git"],
)
print(len(matches))

Apply one or more find-and-replace edits to a single file. The response includes a unified diff and the total number of replacements made.

from leap0 import FileEdit, Leap0Client
client = Leap0Client()
sandbox = client.sandboxes.create()
result = sandbox.filesystem.edit_file(
path="/workspace/main.py",
edits=[
FileEdit(find="hello", replace="hi"),
FileEdit(find="world", replace="earth"),
],
)
print(result.diff)
print(result.replacements)

Replace text across multiple files at once.

from leap0 import Leap0Client
client = Leap0Client()
sandbox = client.sandboxes.create()
results = sandbox.filesystem.edit_files(
paths=["/workspace/a.py", "/workspace/b.py"],
find="old_name",
replace="new_name",
)
print(len(results))

Move or rename a file or directory.

from leap0 import Leap0Client
client = Leap0Client()
sandbox = client.sandboxes.create()
sandbox.filesystem.move(
src_path="/workspace/old.txt",
dst_path="/workspace/new.txt",
overwrite=False,
)

Copy a file or directory. Set recursive to copy directories with contents.

from leap0 import Leap0Client
client = Leap0Client()
sandbox = client.sandboxes.create()
sandbox.filesystem.copy(
src_path="/home/user/file.txt",
dst_path="/home/user/file-copy.txt",
recursive=False,
overwrite=False,
)

Check whether a path exists in the sandbox.

from leap0 import Leap0Client
client = Leap0Client()
sandbox = client.sandboxes.create()
print(sandbox.filesystem.exists(path="/workspace/main.py"))

Get a recursive directory tree. Use exclude to skip directories and max_depth to limit traversal depth. The response is JSON: {"items": [{"name": "src", "type": "directory", "children": [...]}, ...]}.

from leap0 import Leap0Client
client = Leap0Client()
sandbox = client.sandboxes.create()
tree = sandbox.filesystem.tree(
path="/workspace",
exclude=["node_modules", ".git"],
max_depth=3,
)
print(tree.items)