(Lua 5.2) 无法创建文件,因为io.open返回nil文件句柄

huangapple go评论44阅读模式
英文:

(Lua 5.2) Cannot create file because io.open returns nil file handle

问题

我正在尝试在Lua中创建一个文件。

以下这些帖子都说相同的事情:

  1. https://forum.rainmeter.net/viewtopic.php?t=10024
  2. https://stackoverflow.com/questions/6889589/create-a-new-file-in-lua-luafilesystem
  3. https://stackoverflow.com/questions/21528105/sifteo-how-to-create-a-file-txt-with-lua-script
  4. 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:

  1. https://forum.rainmeter.net/viewtopic.php?t=10024
  2. https://stackoverflow.com/questions/6889589/create-a-new-file-in-lua-luafilesystem
  3. https://stackoverflow.com/questions/21528105/sifteo-how-to-create-a-file-txt-with-lua-script
  4. https://stackoverflow.com/questions/7277388/creating-new-files-with-lua-i-o-functions

They say to use:

local file = io.open(&quot;test.txt&quot;, &quot;w&quot;)
file:write(&quot;Hello World&quot;)
file:close()

I have implemented this like so:

local ADDRESSES = &quot;capi/addresses.cfg&quot;

local file, err = io.open(ADDRESSES, &quot;w&quot;)
local data = &quot;&lt;DATA&gt;&quot;
file:write(data)
file:close()

This, however, results in the error Attempt to index local &#39;file&#39; (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")将在文件不存在的情况下创建文件。

但是,文件操作通常需要满足至少三个前提条件:

  1. 您必须具有足够的权限在所需路径上创建文件(例如,在文件夹中具有写入权限)。
  2. 沿路径的所有文件夹必须已经存在。Lua 不会为您创建文件夹。
  3. 必须有足够的空间(您的文件系统可能已满)。

您可能没有满足其中一个前提条件。要找出是哪个前提条件,请将您对 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, &quot;w&quot;) will create the file if it doesn't already exist.

However, there are at least three prerequisites common to file operations:

  1. You must have sufficient permissions to create the file in the desired path (e.g. write permission in the folder)
  2. All folders along the path must already exist. Lua will not create folders for you.
  3. 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, &quot;w&quot;))

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.

huangapple
  • 本文由 发表于 2023年2月9日 02:18:59
  • 转载请务必保留本文链接:https://go.coder-hub.com/75390117.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定