Lua io.type()检查目录是否存在

openresty 2023-08-29 720次浏览

io.type() 函数不能直接用于检查一个路径是否为目录。

io.type() 的作用是检测一个文件/路径的类型,它会返回以下类型字符串中的一个:

  • "file" - 普通文件
  • "directory" - 目录
  • "socket" - Unix domain socket
  • "char" - 字符设备文件
  • "pipe" - 管道
  • "fifo" - FIFO
    但是,对于不存在的路径,io.type() 会返回 nil。

例如:

local path = "/path/to/dir"

if io.type(path) == nil then
    print("路径不存在")
else
    print(io.type(path))
end

上面代码如果路径不存在,io.type() 返回的是 nil。

所以 io.type() 并不能直接用于检查路径是否存在,因为不存在路径和目录类型都会返回 nil。

Lua io.type()检查目录是否存在

喜欢 (0)
发表我的评论
取消评论
表情 贴图 加粗 删除线 居中 斜体 签到

Hi,您需要填写昵称和邮箱!

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址