在Graal VM中使用Python创建/读取文件

2024-10-02 10:21:59 发布

您现在位置:Python中文网/ 问答频道 /正文

我使用Java和Python等组合语言与Graal VM合作。我在尝试使用context.eval()执行Python sintax读取/创建文件时遇到问题

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

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

并且可以工作,但当我尝试运行代码以使用Java读取context.eval()中的文件时:

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

这给我带来了一个错误:

许可错误:(1,不允许操作,cadena.txt,无,无)

我也试着用sudo和sudo su运行它,但它给了我同样的错误。有人知道为什么会这样吗

谢谢


Tags: 文件代码txt语言close错误evalcontext
1条回答
网友
1楼 · 发布于 2024-10-02 10:21:59

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

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

对于试验/原型设计而言,允许以下各项可能很有用:

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

相关问题 更多 >

    热门问题