文件系统 前提条件 在控制台完成沙箱模板创建。 在执行下文所有代码前,请先按照环境变量设置部分,完成环境变量设置。 文件读取与下载 从沙箱实例中读取文件内容,或下载文件到本地。 python from e2bcodeinterpreter import Sandbox 注意:修改下面模板为您的模板名称或者模板id sandbox Sandbox.create(template"mytestcodetemplate") 从沙箱实例中读取文件 filecontent sandbox.files.read("yourfilepath") 从沙箱实例中下载文件到本地 with open("localpath","wb") as file: 以二进制形式读取文件 file.write(sandbox.files.read("yourfilepath","bytes")) 文件写入与上传 您可以使用 files 类的 write 和 writefiles 方法向沙箱实例中写入文件。 python from e2bcodeinterpreter import Sandbox 注意:修改下面模板为您的模板名称或者模板id sandbox Sandbox.create(template"mytestcodetemplate") 向沙箱实例中写入单个文件 sandbox.files.write("yourfilepath","filecontent") 向沙箱实例中上传文件 with open("localpath","rb") as file: 以二进制形式上传文件 sandbox.files.write("yourfilepath",file) 向沙箱实例中写入多个文件 sandbox.files.writefiles( [ {"path": "yourpath1", "data": "filecontent"}, {"path": "yourpath2", "data": "filecontent"}, ] ) 检查文件是否存在 您可以使用 files 类的 exists 方法来检测沙箱实例中是否存在某个文件。 python response sandbox.files.exists("filename") 存在 True 不存在 False 重命名文件 您可以使用 files 类的 rename 方法来重命名沙箱实例中的某个文件。 python response sandbox.files.rename("filenamebefore","filenameafter")