Lua loadfile找不到文件 [英] Lua loadfile not finding a file

查看:667
本文介绍了Lua loadfile找不到文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在下面的行中有一些lua代码:

I had some lua code with the following line:

JSON = loadfile("JSON.lua")()

文件JSON.lua与该行的lua代码位于同一目录中.这段代码为我工作了一会儿,然后,在不更改lua源或JSON.lua或任何文件的许可或运行lua代码的目录的情况下,我开始得到一个该行没有错误. (我只是简单地记得没有可能对lua代码有任何影响的相关更改.)

The file JSON.lua is in the same directory as the lua code that line came from. This code worked for me for a while, and then, without my changing either the lua source, or the JSON.lua, or permission of any of the files, or the directory from where I was running the lua code, I started getting a nil error on that line. (I simply recall NO relevant changes that could have any impact on the lua code.)

添加一个断言表明该错误是由找不到文件引起的.使用文件权限,重新启动计算机无法解决问题,并且撤回已签入并正常运行的代码无法解决该错误.

Adding an assert revealed that the error was caused by the file not being found. Playing with file permissions, restarting my machine didn't resolve the issue, and pulling back code that I had checked in and was working perfectly did not resolve the error.

我通过更改上面的行以提供该JSON.lua文件的绝对路径来解决了该错误.

I resolved the error by changing the line above to provide the absolute path to that JSON.lua file.

有什么解释为什么没有绝对路径的代码可以工作一段时间然后停止工作?

Is there anything explaining why the code without the absolute path could have worked for a while and then stopped working?

注意:这种正常工作然后又不工作的行为在一周内两次发生在我身上.我很困惑,尽管我现在已经找到了解决方法,但我对于这种间歇性行为的解释感到非常好奇.

Note: This behavior of working and then not working happened to me twice over a week. I am puzzled and though I have now found a fix, I am really curious as to the explanation for that intermittent behavior.

推荐答案

Lua使用 package.path ,其默认值来自环境变量LUA_PATH(如果已设置)作为要搜索的目录列表.您可以在此列表的前面放置.,以从当前目录加载文件,也可以将文件放在列表中的路径中.

Lua uses package.path, whose default value comes from the environment variable LUA_PATH if it is set, as the list of directories to search. You can put . of the front of this list to load files from the current directory, or you can put your files in a path on the list.

这篇关于Lua loadfile找不到文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆