使用Lua,检查文件是否为目录 [英] Using Lua, check if file is a directory

查看:737
本文介绍了使用Lua,检查文件是否为目录的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果我有此代码

local f = io.open("../web/", "r")
print(io.type(f))

-- output: file

我怎么知道f是否指向目录?

how can I know if f points to a directory?

推荐答案

Lua的默认库无法确定这一点.

Lua's default libraries don't have a way to determine this.

但是,您可以使用第三方 LuaFileSystem 库来访问更高级的文件-系统交互;它也是跨平台的.

However, you could use the third-party LuaFileSystem library to gain access to more advanced file-system interactions; it's cross-platform as well.

LuaFileSystem提供lfs.attributes,可用于查询文件模式:

LuaFileSystem provides lfs.attributes which you can use to query the file mode:

require "lfs"
function is_dir(path)
    -- lfs.attributes will error on a filename ending in '/'
    return path:sub(-1) == "/" or lfs.attributes(path, "mode") == "directory"
end

这篇关于使用Lua,检查文件是否为目录的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆