如果我有这个代码
local f = io.open("../web/", "r")
print(io.type(f))
-- output: file
我如何知道f
是否指向一个目录?
最佳答案
ANSI C 没有指定任何获取目录信息的方法,因此普通 Lua 无法告诉您该信息(因为 Lua 力求 100% 可移植性)。但是,您可以使用外部库,例如 LuaFileSystem识别目录。
Progamming in Lua甚至明确指出缺少的目录功能:
As a more complex example, let us write a function that returns the contents of a given directory. Lua does not provide this function in its standard libraries, because ANSI C does not have functions for this job.
该示例将向您展示如何自己用 C 语言编写 dir
函数。
关于lua - 使用Lua检查文件是否是目录,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2833675/