javapythonfilegraalvmgraalpython

Create/Read files with Python in Graal VM


Im working with Graal VM, using combined languages like Java and Python. I have a problem when try to execute Python sintax to read/create files using context.eval().

I use this code using Graalpython in terminal:

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

and works, but when I tried to run a code to read the file in context.eval() with Java:

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

it throws me this error:

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

I also tried running it using sudo and sudo su but it gives me the same error. Does anyone know why this happened?

Thanks


Solution

  • You need to give your context permission to do IO:

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

    For experimenting/prototyping it may be useful to allow everything:

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