如何通过路径在Lua中提取出文件名?

2026-04-01 19:291阅读0评论SEO教程
  • 内容介绍
  • 文章标签
  • 相关推荐

本文共计160个文字,预计阅读时间需要1分钟。

如何通过路径在Lua中提取出文件名?

使用lfs工具遍历指定路径下的文件,获取文件名,排除当前目录和父目录,并获取其属性。代码如下:

bashrequire lfsfunction dirpath(path) for file in lfs.dir(path) if file ~=. and file ~=.. then local f=(path .. / .. file) local attr=lfs.attributes(f) print(文件名: .. file .. ,属性: .. attr) end endend

require "lfs" function dirpath(path) for file in lfs.dir(path) do -- lfs.dir 根据路径获取该路径下的文件名 if file ~= ‘.‘ and file ~= ‘..‘ then local f = (path .. ‘/‘..file) local attr = lfs.attributes(f) -- 该文件的各种属性 if attr.mode == "directory" then print(f .. " --> " .. attr.mode) dirpath(f) else print(f .. " --> " .. attr.mode) end end end end dirpath("/usr")

删除文件: os.remove(filepath)

如何通过路径在Lua中提取出文件名?

本文共计160个文字,预计阅读时间需要1分钟。

如何通过路径在Lua中提取出文件名?

使用lfs工具遍历指定路径下的文件,获取文件名,排除当前目录和父目录,并获取其属性。代码如下:

bashrequire lfsfunction dirpath(path) for file in lfs.dir(path) if file ~=. and file ~=.. then local f=(path .. / .. file) local attr=lfs.attributes(f) print(文件名: .. file .. ,属性: .. attr) end endend

require "lfs" function dirpath(path) for file in lfs.dir(path) do -- lfs.dir 根据路径获取该路径下的文件名 if file ~= ‘.‘ and file ~= ‘..‘ then local f = (path .. ‘/‘..file) local attr = lfs.attributes(f) -- 该文件的各种属性 if attr.mode == "directory" then print(f .. " --> " .. attr.mode) dirpath(f) else print(f .. " --> " .. attr.mode) end end end end dirpath("/usr")

删除文件: os.remove(filepath)

如何通过路径在Lua中提取出文件名?