|
| 1 | +--- |
| 2 | +title: File operations |
| 3 | +description: Learn how to read, write, and mount files in W&B Sandboxes. |
| 4 | +--- |
| 5 | + |
| 6 | +<Warning> |
| 7 | +W&B Sandboxes is in private preview, available by invitation only. To request enrollment, contact [support](mailto:support@wandb.com) or your AISE. |
| 8 | +</Warning> |
| 9 | + |
| 10 | +Use file operations to share data between your local environment and a sandbox. You can read files from a sandbox, write files to it, or mount local files and directories into it. |
| 11 | + |
| 12 | +For example, you can write a Python script to a sandbox, run it, and read the output file back to your local environment. You can also mount a directory of training data into a sandbox for a machine learning job. |
| 13 | + |
| 14 | +<Tip> |
| 15 | +**Choose the right file access method** |
| 16 | + |
| 17 | +Mount files or directories when you want the sandbox to access local data without copying it. |
| 18 | + |
| 19 | +Read and write files when you want to transfer smaller files between your local environment and the sandbox, or when you want to save sandbox output locally. |
| 20 | +</Tip> |
| 21 | + |
| 22 | +## Write a file to the sandbox |
| 23 | + |
| 24 | + |
| 25 | +{/* creating, updating, modifying, or saving data */} |
| 26 | +{/* transfers a file into the sandbox. Use it to upload inputs your sandbox code needs (scripts, configs, data files). */} |
| 27 | + |
| 28 | +Transfer a file from your local environment to the sandbox using the [`Sandbox.write_file()`](https://docs.coreweave.com/products/coreweave-sandbox/client/ref/core/sandbox#write_file) method. |
| 29 | + |
| 30 | + |
| 31 | +```python |
| 32 | +from pathlib import Path |
| 33 | +from wandb.sandbox import Sandbox |
| 34 | + |
| 35 | +# Path of the local file you want to write to the sandbox |
| 36 | +text_file = Path("hello.txt") |
| 37 | + |
| 38 | +with Sandbox.run() as sandbox: |
| 39 | + # Write a file to the sandbox |
| 40 | + sandbox.write_file("hello.txt", text_file.read_bytes()).result() |
| 41 | +``` |
| 42 | + |
| 43 | +See the `Sandbox` class reference documentation for a full list of parameters and options for [`Sandbox.write_file()`](https://docs.coreweave.com/products/coreweave-sandbox/client/ref/core/sandbox#write_file). |
| 44 | + |
| 45 | +## Read a file from the sandbox |
| 46 | + |
| 47 | +Save a file from the sandbox to your local environment using the [`Sandbox.read_file()`](https://docs.coreweave.com/products/coreweave-sandbox/client/ref/core/sandbox#read_file) method. |
| 48 | + |
| 49 | +```python |
| 50 | +from pathlib import Path |
| 51 | +from wandb.sandbox import Sandbox |
| 52 | + |
| 53 | +with Sandbox.run() as sandbox: |
| 54 | + # Demo creates a file in the sandbox to read back |
| 55 | + sandbox.exec(["sh", "-c", "echo 'Hello, world.' > hello.txt"]).result() |
| 56 | + |
| 57 | + # Save the file on local machine (not sandbox) |
| 58 | + content = sandbox.read_file("hello.txt").result() # b"Hello, world.\n" |
| 59 | + Path("hello.txt").write_bytes(content) |
| 60 | +``` |
| 61 | + |
| 62 | +See the `Sandbox` class reference documentation for a full list of parameters and options for [`Sandbox.read_file()`](https://docs.coreweave.com/products/coreweave-sandbox/client/ref/core/sandbox#read_file). |
| 63 | + |
| 64 | + |
| 65 | +## Mount a file or directory |
| 66 | + |
| 67 | +Use mounted files to provide local files to the sandbox at creation time. Unlike `Sandbox.write_file()`, which transfers files to a running sandbox, mounted files are available as soon as the sandbox starts. Mounted files appear in the sandbox at the path you specify. |
| 68 | + |
| 69 | +<Info> |
| 70 | +Mounted files are read-only in the sandbox. If you need to modify files in the sandbox, use `Sandbox.write_file()` instead. |
| 71 | +</Info> |
| 72 | + |
| 73 | +In the following code snippet, local files `train.py` and `requirements.txt` are mounted to the sandbox root directory. The sandbox installs dependencies from `requirements.txt` and then runs `train.py`. |
| 74 | + |
| 75 | +```python |
| 76 | +from pathlib import Path |
| 77 | +from wandb.sandbox import Sandbox, NetworkOptions |
| 78 | + |
| 79 | +mounted_files = [ |
| 80 | + {"mount_path": "requirements.txt", "file_content": Path("requirements.txt").read_bytes()}, |
| 81 | + {"mount_path": "train.py", "file_content": Path("train.py").read_bytes()}, |
| 82 | + ] |
| 83 | + |
| 84 | +print("Starting sandbox...") |
| 85 | +with Sandbox.run(mounted_files=mounted_files) as sandbox: |
| 86 | + |
| 87 | + # The mounted files are available in the sandbox at the specified mount paths |
| 88 | + print("Installing dependencies...") |
| 89 | + result = sandbox.exec(["pip", "install", "-r", "requirements.txt"], check=True).result() |
| 90 | + print(result.stdout) |
| 91 | + |
| 92 | + print("Running script...") |
| 93 | + result = sandbox.exec(["python", "train.py"]).result() |
| 94 | + print(result.stdout) |
| 95 | +``` |
0 commit comments