andlua+怎么读取指定目录的每个文件和文件夹并显示在页面中?
以下是使用Lua和LuaFileSystem库读取指定目录的每个文件和文件夹,并显示在页面中的示例代码:
local lfs = require("lfs")
-- 指定目录路径
local path = "/path/to/directory"
-- 遍历目录
for file in lfs.dir(path) do
-- 排除 . 和 .. 目录
if file ~= "." and file ~= ".." then
-- 获取文件/目录的完整路径
local filepath = path .. "/" .. file
-- 判断是否为文件夹
if lfs.attributes(filepath, "mode") == "directory" then
print("文件夹:" .. file)
else
print("文件:" .. file)
end
end
end
在上述示例代码中,我们使用了LuaFileSystem库中的lfs.dir()和lfs.attributes()函数来遍历指定目录下的每个文件和目录,并获取它们的完整路径和类型。如果它是一个文件夹,则输出“文件夹:[目录名]”;如果是一个文件,则输出“文件:[文件名]”。您可以根据需要将输出语句更改为在页面中显示文件和目录的名称
原文地址: https://www.cveoy.top/t/topic/hcSu 著作权归作者所有。请勿转载和采集!