Lua开源库 lfs的介绍与使用
来源:互联网 发布:python google语音api 编辑:程序博客网 时间:2024/06/12 03:59
Lua lfs库
这个库可以实现平台无关(Linux和Windows通用)的文件系统访问
官网地址
Github项目地址
如何配置:
5.1以上的lua已经包含了lfs库,路径是Lua5.1\clibs\lfs.dll,我们无需配置直接require “lfs”使用即可。
提供的功能:
lfs的开发提供了功能的介绍:官方手册
下面给出精简了内容的中文版(方便不喜欢看英文版的同学):
- lfs.attributes (filepath [, aname])
返回这个path的属性table,如果filepath是nil则会出错并打印错误信息,属性列表如下所示:
mode属性是字符串,其他属性都是数组。
- lfs.chdir (path)
将当前目录改为给定的path
- lfs.currentdir ()
获取当前目录
- lfs.dir (path)
Lua遍历目录下的所有入口,每次迭代都返回值为入口名的字符串
- lfs.lock (filehandle, mode[, start[, length]])
锁定一个文件或这文件的部分内容
- lfs.mkdir (dirname)
创建一个目录
- lfs.rmdir (dirname)
移除一个已存在的目录
- lfs.setmode (file, mode)
设置文件的写入模式,mode字符串可以是binary或text
- lfs.symlinkattributes (filepath [, aname])
比lfs.attributes多了the link itself (not the file it refers to)信息,其他都和lfs.attribute一样
- lfs.touch (filepath [, atime [, mtime]])
设置上一次使用和修改文件的时间值
- lfs.unlock (filehandle[, start[, length]])
解锁文件或解锁文件的部分内容
小实例 传入一个根目录路径,递归获取该路径子目录的所有文件全路径:
local lfs = require "lfs"local allFilePath = {}function main() -- 打印lfs库的版本 PrintLfsVersion() -- 打印当前目录地址 PrintCurrrentDir() -- 打印当前目录的下一层子目录地址 local rootPath = lfs.currentdir() PrintDirChildren(rootPath) -- 递归打印当前目录下面所有层级的子目录并将文件地址保存到表中 GetAllFiles(rootPath) PrintTable(allFilePath)endfunction PrintLfsVersion( ... ) print(lfs._VERSION)endfunction PrintCurrrentDir( ... ) local rootPath = lfs.currentdir() print(rootPath)endfunction PrintDirChildren(rootPath) for entry in lfs.dir(rootPath) do if entry~='.' and entry~='..' then local path = rootPath.."\\"..entry local attr = lfs.attributes(path) assert(type(attr)=="table") --如果获取不到属性表则报错 -- PrintTable(attr) if(attr.mode == "directory") then print("Dir:",path) elseif attr.mode=="file" then print("File:",path) end end endendfunction GetAllFiles(rootPath) for entry in lfs.dir(rootPath) do if entry~='.' and entry~='..' then local path = rootPath.."\\"..entry local attr = lfs.attributes(path) assert(type(attr)=="table") --如果获取不到属性表则报错 -- PrintTable(attr) if(attr.mode == "directory") then -- print("Dir:",path) GetAllFiles(path) --自调用遍历子目录 elseif attr.mode=="file" then -- print(attr.mode,path) table.insert(allFilePath,path) end end endendfunction PrintTable( tbl , level, filteDefault) local msg = "" filteDefault = filteDefault or true --默认过滤关键字(DeleteMe, _class_type) level = level or 1 local indent_str = "" for i = 1, level do indent_str = indent_str.." " end print(indent_str .. "{") for k,v in pairs(tbl) do if filteDefault then if k ~= "_class_type" and k ~= "DeleteMe" then local item_str = string.format("%s%s = %s", indent_str .. " ",tostring(k), tostring(v)) print(item_str) if type(v) == "table" then PrintTable(v, level + 1) end end else local item_str = string.format("%s%s = %s", indent_str .. " ",tostring(k), tostring(v)) print(item_str) if type(v) == "table" then PrintTable(v, level + 1) end end end print(indent_str .. "}")endmain()
参考:
lua使用lfs.dll库进行文件操作
- Lua开源库 lfs的介绍与使用
- Lua ---- LFS库的使用
- Lua ---- LFS库的使用
- Lua ---- LFS库的使用
- Lua ---- LFS库的使用
- lua lfs库的使用,递归获取子文件路径
- lfs的介绍
- lua lfs
- 【Lua】使用lfs遍历文件目录
- git lfs的使用
- Lua的介绍和基本使用
- lfs 使用
- 使用LFS打造自己的Linux心得体会
- cocos2d-lua 添加lfs模块
- Lua 的简单介绍
- Lua的入门介绍
- Lua 的简单介绍
- Lua&tolua++与c++的使用
- C++将csv文件数据读入数组中
- hiho第173周 A Game (区间DP)
- gsoap_2.8.33.zip安装与编译
- 前端开发规范整理
- 深度学习的简单理解
- Lua开源库 lfs的介绍与使用
- [VijosP1737][NOIp2011tg D1T2]选择客栈
- ccf 201509-2日期计算
- spring中@value注解需要注意
- 子窗体透明度问题
- 在C#中调用Java生成的Jar库文件的方法
- JDBC连接数据库
- 【入门】Java取火柴游戏
- 登录脚本的参数化