Create/Read 个文件 Python 在 Graal VM 中

Create/Read files with Python in Graal VM

我正在使用 Graal VM,使用 Java 和 Python 等组合语言。我在尝试使用 context.eval().

对 read/create 文件执行 Python sintax 时遇到问题

我在终端中使用 Graalpython 使用此代码:

out_file = File.new("cadena.txt", "w+")
out_file.puts("write your stuff here")
out_file.close 

并且有效,但是当我尝试 运行 一个代码来读取 context.eval() 中的文件时 Java:

codigoPython += "fichw = open('cadena.txt','r')";
codigoPython += "fichw.read() ";
codigoPython += "fichw.close() ";
Value filecontent = context.eval("python", codigoPython);

它抛出这个错误:

PermissionError: (1, Operation not permitted, cadena.txt, None, None)

我也尝试 运行使用 sudo 和 sudo su 对其进行调试,但它给了我同样的错误。有谁知道为什么会这样?

谢谢

您需要授予上下文权限才能执行 IO:

Context context = Context.newBuilder("python").allowIO(true).build();

对于experimenting/prototyping,允许所有内容可能会有用:

Context context = Context.newBuilder("python").allowAllAccess(true).build();