英文:
(Lua 5.2) Cannot create file because io.open returns nil file handle
问题
我正在尝试在Lua中创建一个文件。
以下这些帖子都说相同的事情:
- https://forum.rainmeter.net/viewtopic.php?t=10024
- https://stackoverflow.com/questions/6889589/create-a-new-file-in-lua-luafilesystem
- https://stackoverflow.com/questions/21528105/sifteo-how-to-create-a-file-txt-with-lua-script
- https://stackoverflow.com/questions/7277388/creating-new-files-with-lua-i-o-functions
它们都建议使用以下代码:
local file = io.open("test.txt", "w")
file:write("Hello World")
file:close()
我已经按照这样的方式实现了它:
local ADDRESSES = "capi/addresses.cfg";
local file, err = io.open(ADDRESSES, "w")
local data = "<DATA>"
file:write(data)
file:close()
然而,这导致错误尝试索引本地'file'(一个空值)
。这当然是因为文件不存在,因为我试图创建它。这些示例似乎说当我尝试写入它时,文件应该会自动创建,但如果句柄为nil
,我该如何做呢?
英文:
I am trying to create a file in Lua.
The following threads all say the same thing:
- https://forum.rainmeter.net/viewtopic.php?t=10024
- https://stackoverflow.com/questions/6889589/create-a-new-file-in-lua-luafilesystem
- https://stackoverflow.com/questions/21528105/sifteo-how-to-create-a-file-txt-with-lua-script
- https://stackoverflow.com/questions/7277388/creating-new-files-with-lua-i-o-functions
They say to use:
local file = io.open("test.txt", "w")
file:write("Hello World")
file:close()
I have implemented this like so:
local ADDRESSES = "capi/addresses.cfg"
local file, err = io.open(ADDRESSES, "w")
local data = "<DATA>"
file:write(data)
file:close()
This, however, results in the error Attempt to index local 'file' (a nil value)
. This is, of course, because the file does not exist because I am trying to create it. The examples seem to say that the file should be automatically created when I try to write to it, but how can I do that if the handle is nil
!?
答案1
得分: 2
正确的是io.open(filename, "w")
将在文件不存在的情况下创建文件。
但是,文件操作通常需要满足至少三个前提条件:
- 您必须具有足够的权限在所需路径上创建文件(例如,在文件夹中具有写入权限)。
- 沿路径的所有文件夹必须已经存在。Lua 不会为您创建文件夹。
- 必须有足够的空间(您的文件系统可能已满)。
您可能没有满足其中一个前提条件。要找出是哪个前提条件,请将您对 io.open
的调用包装在 assert
中:
local file = assert(io.open(ADDRESSES, "w"))
现在,如果打开/创建文件失败,Lua 将引发一个错误,告诉您未能满足哪个前提条件。
在您的情况下,我认为最有可能的是 capi
目录尚不存在。在我的 Linux 系统上,相应的错误消息是 capi/addresses.cfg: No such file or directory
。
英文:
It is correct that io.open(filename, "w")
will create the file if it doesn't already exist.
However, there are at least three prerequisites common to file operations:
- You must have sufficient permissions to create the file in the desired path (e.g. write permission in the folder)
- All folders along the path must already exist. Lua will not create folders for you.
- There must be sufficient space (your file system may not be full)
You are presumably not meeting one of the prerequisites. To find out which, simply wrap your call to io.open
with assert
:
local file = assert(io.open(ADDRESSES, "w"))
Now if opening/creating the file fails, Lua will throw an error that tells you which prerequisite you failed to meet.
In your case, I would consider it most probable that the capi
directory doesn't exist yet. On my Linux system, the corresponding error message is capi/addresses.cfg: No such file or directory
.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论