lua - 检查目录是否存在于lua中?

标签 lua io filesystems

如何检查 lua 中是否存在目录,如果可能的话最好不使用 LuaFileSystem 模块?

尝试做这样的 python 行:

os.path.isdir(path)

最佳答案

这是一种适用于 Unix 和 Windows 的方式,无需任何外部依赖:

--- Check if a file or directory exists in this path
function exists(file)
   local ok, err, code = os.rename(file, file)
   if not ok then
      if code == 13 then
         -- Permission denied, but it exists
         return true
      end
   end
   return ok, err
end

--- Check if a directory exists in this path
function isdir(path)
   -- "/" works on both Unix and Windows
   return exists(path.."/")
end

关于lua - 检查目录是否存在于lua中?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1340230/

相关文章:

macos - 如何修改 Mac OS 中文件系统缓存的行为

linux - 从内核驱动程序调用同步

lua - 存储为字符串的简单 Lua 表的反序列化

c++ - 在不执行脚本的情况下调用 Lua 函数

string - 在 Lua 中使用 string.gmatch() 拆分字符串

c - 在 Windows 上的 C 目录中查找最旧的文件

java - 何时使用 Java 中的 Writer 子类;常见做法

linux - "rm"(删除)目录中的 800 万个文件?

string - Lua string.len、string.lower 或任何其他字符串函数不起作用

java - 将依赖注入(inject)与 Apache Commons IO 结合使用