Rename Files

Updated at:

Renaming is useful for moving files, promoting generated results, or switching a temporary file to its final name.

Move or rename a file

Python example:

sandbox.files.write("/tmp/result.tmp", "done\n")
sandbox.files.rename("/tmp/result.tmp", "/tmp/result.txt")

TypeScript example:

await sandbox.files.write("/tmp/result.tmp", "done\n");
await sandbox.files.rename("/tmp/result.tmp", "/tmp/result.txt");

You can also use it to move directories:

Python example:

sandbox.files.rename("/tmp/build", "/tmp/build-ready")

TypeScript example:

await sandbox.files.rename("/tmp/build", "/tmp/build-ready");

Recommendations

For important outputs, write to a temporary file first and then switch it to the final file name with rename(). This reduces the chance that readers will observe a partially written result.