Lua 检查指定路径是否是一个目录

openresty 2023-08-29 709次浏览

在Lua中,有几种方法可以检查指定路径是否是目录:

  1. 使用io.type()函数:
local path = "/path/to/dir"

if io.type(path) == "directory" then
    print(path, "is a directory")
else
    print(path, "is not a directory")
end
  1. 检查路径最后一个字符是否为'/'
if path:sub(-1) == "/" then
    print("是目录")
else
    print("不是目录")
end
  1. 尝试创建目录,如果已存在则是目录:
local ok, err = os.mkdir(path)
if not ok then
    if err == 13 then -- errno.EEXIST
        print("是目录") 
    else
        print("不是目录")
    end
end

Lua 检查指定路径是否是一个目录

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

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

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