lua获取文件名|linux平台:使用lua语言遍历某一文件夹下所有文件

lua获取文件名|linux平台:使用lua语言遍历某一文件夹下所有文件的第1张示图

❶ lua 如何获取某个文件夹的所有文件的列表

local lfs = require"lfs"function attrdir (path) for file in lfs.dir(path) do if file ~= "." and file ~= ".." then local f = path..'/'..file local attr = lfs.attributes (f) assert (type(attr) == "table") if attr.mode == "directory" thenprint(f)end end endendattrdir ("E:\\LUA")

❷ 刚学的LUA,有的地方不明白,向各位高手求教不明白下面的代码,能详细说明下吗

arg表示lua函数被调用时 的参数你只要展开一下大概就知道是什么了比如我这里这一段代码for i,v in pairs(arg) doprint(v)end运行后可以看到-e.lualuaio.stdout:setvbuf 'no'我们就可以知道我们的lua解释器的名字为lua,lua文件当前文件名为.lua你也可以到CMD下运行 lua文件 会发现不一样的结果 总之arg参数就是描述你调用脚本时的各个参数 写lua脚本的话 一定要有个好的缩进 不然会导致代码的误解printResult = "" –定义一个空字符串function print(…) –重新写一个print函数for i,v in ipairs(arg) do –函数的运行内容为遍历arg的所有参数 具体的实现方法为 将所有的字符串连接起来 最后再输出printResult = printResult .. tostring(v) .. "\t"endprintResult = printResult .. "\n"end这个代码大体上思路是没问题 但是有一个地方做的不好就是 要输出的内容 如果为多个字符串的话 就比如你这里 是arg的多个参数你这里是把所有的字符串连结到一起再输出 实际的话 你这里多了一个连接的操作不如直接使用io.write 或者将所有的字符串保存在一个table里 最后使用 table.concat连接输出因为当字符串的组成项过多的时候 连接字符串的代价就非常大了大个比方 将1M 个1B 的字符串 连接起来要执行1M次 光是内存的占用量就达到0.5MB*1MB 已经是接近0,5G的内存调用内存的执行非常没效率 很浪费时间和资源

❸ lua语言 如何读取一个文件中的内容(里面是应用路径),然后打开对应的文件。

第一步:先把txt文件复制到MATLAB的目录,或者在MATLAB中将路径指向txt文件所在路径。第二步:右键存有数据的txt文件,选择Import Data…第三步:Import Data之后就能看到txt里的数据被妥善安放好位置了,然后在Range右边的列表中选择Matrix,再点击绿色的对勾√导入数据:第四步:导入完数据后,在workplace里能看到名为txt文件名的数组变量,就说明导入成功,这里是a:第五步:最后就是编写语句了:plot(a(:,2),a(:,3),'o'),回车就会出现以o为点的散点图,如果是:plot(a(:,2),a(:,3),'*'),就得到以*为点的散点图;绘图说明:1.将数据表的各列数值分别赋予变量x、y、z等,格式如下:x=sheetname(:,1), y=sheetname(:,2), z=sheetname(:,3);2.用命令plot(x,y,’XXXX’)绘制图形,单引号中的符号表示点线的属性,如线形、颜色、点的形状等,若用双对数坐标画图则命令为loglog(x,y);3.在弹出的绘图界面中用菜单View—Property Editor编辑图形属性,如字体大小、数据点形状、横纵坐标名称、绘图区域颜色等;4.绘图方法2:在数组编辑器上点击Plot Selection按钮,选择图形的类型即可;5.绘图方法3:菜单File—New—Figure创建新的图形,在图形编辑器中Figure Palette面板点击2D Axes,点击右下角Add Data选择图表类型和坐标轴的数据源,度分布图将坐标轴由线形改为对数即可。6.hold on/off命令:叠绘命令,切换绘图的保持功能;7.绘制双纵轴:7.1 plotyy(x1,y1,x2,y2):分别用左/右侧y轴表示两条曲线;7.2 plotyy(x1,y1,x2,y2,FUN):FUN是字符串格式,用来指定绘图的函数名,可以由多个。8.创建子图:subplot(m,n,p):表示将绘图区域分为m*n个矩形块,分别创建坐标系,如 >>subplot(1,2,1);loglog(x,y); >>subplot(1,2,2);loglog(m,n); 表示在图形中创建两个子图,左右排列,分别绘制双对数坐标图,变量分别是x、y和m、n。

❹ lua中根据文件名 获取文件的绝对路径

^fn_flag = string.find(filename, "\\")if fn_flag thendest_filename = string.match(filename, ".+\\([^\\]*%.%w+)$")endfn_flag = string.find(filename, "/")if fn_flag thendest_filename = string.match(filename, ".+/([^/]*%.%w+)$")end更多详见 http://blog.csdn.net/bull_liu/article/details/8726089

❺ lua 语言中filename在开头经常出现,我想知道它的基本含义

filename——一般是表示文件名的字符串变量,可以是带绝对路径或者相对路径的文件名字符串。

❻ 怎样使用lua读取文件夹内所有文件

3年半了都没人回答你,我来帮你关闭吧

❼ lua如何读取文件名称

local str = "aaa.bbb.bbb.txt"–获取文件名function getFileName(str) local idx = str:match(".+()%.%w+$") if(idx) then return str:sub(1, idx-1) else return str endend–获取扩展名function getExtension(str) return str:match(".+%.(%w+)$")endngx.say("源字符串:"..str)ngx.say("====================")ngx.say("文件名:"..getFileName(str))ngx.say("====================")ngx.say("扩展名:"..getExtension(str))

❽ 求Lua插件枚举安卓系统指定目录下文件名实例

用lua file system 这个库,在lua项目主页可以找到,或者去谷歌。具体用法在它的说明文档里有。你可以看这个库的例子,里面有教你查看文件的创建日期与修改日期。下面是例子:——————————————————local tmp = "/tmp"local sep = "/"local upper = ".."require"lfs"print (lfs._VERSION)function attrdir (path) –这个就是你要的目录迭代器for file in lfs.dir(path) doif file ~= "." and file ~= ".." thenlocal f = path..sep..fileprint ("\t=> "..f.." <=")–这一行打印出所有文件,你可以改成你想要的local attr = lfs.attributes (f)assert (type(attr) == "table")if attr.mode == "directory" thenattrdir (f)elsefor name, value in pairs(attr) doprint (name, value)endendendendend—下面是其他的例子,如获取文件属性– Checking changing directorieslocal current = assert (lfs.currentdir())local reldir = string.gsub (current, "^.*%"..sep.."([^"..sep.."])$", "%1")assert (lfs.chdir (upper), "could not change to upper directory")assert (lfs.chdir (reldir), "could not change back to current directory")assert (lfs.currentdir() == current, "error trying to change directories")assert (lfs.chdir ("this couldn*t be an actual directory") == nil, "could change to a non-existent directory")– Changing creating and removing directorieslocal tmpdir = current..sep.."lfs_tmp_dir"local tmpfile = tmpdir..sep.."tmp_file"– Test for existence of a previous lfs_tmp_dir– that may have resulted from an interrupted test execution and remove itif lfs.chdir (tmpdir) thenassert (lfs.chdir (upper), "could not change to upper directory")assert (os.remove (tmpfile), "could not remove file from previous test") assert (lfs.rmdir (tmpdir), "could not remove directory from previous test")end– tries to create a directoryassert (lfs.mkdir (tmpdir), "could not make a new directory")local attrib, errmsg = lfs.attributes (tmpdir)if not attrib thenerror ("could not get attributes of file `"..tmpdir.."*:\n"..errmsg)endlocal f = io.open(tmpfile, "w")f:close()– Change access timelocal testdate = os.time({ year = 2007, day = 10, month = 2, hour=0})assert (lfs.touch (tmpfile, testdate))local new_att = assert (lfs.attributes (tmpfile))assert (new_att.access == testdate, "could not set access time")assert (new_att.modification == testdate, "could not set modification time")– Change access and modification timelocal testdate1 = os.time({ year = 2007, day = 10, month = 2, hour=0})local testdate2 = os.time({ year = 2007, day = 11, month = 2, hour=0})assert (lfs.touch (tmpfile, testdate2, testdate1))local new_att = assert (lfs.attributes (tmpfile))assert (new_att.access == testdate2, "could not set access time")assert (new_att.modification == testdate1, "could not set modification time")local res, err = lfs.symlinkattributes(tmpfile)if err ~= "symlinkattributes not supported on this platform" then– Checking symbolic link information (does not work in Windows)assert (os.execute ("ln -s "..tmpfile.." _a_link_for_test_"))assert (lfs.attributes"_a_link_for_test_".mode == "file")assert (lfs.symlinkattributes"_a_link_for_test_".mode == "link")assert (os.remove"_a_link_for_test_")endif lfs.setmode then– Checking text/binary modes (works only in Windows)local f = io.open(tmpfile, "w")local result, mode = lfs.setmode(f, "binary")assert((result and mode == "text") or (not result and mode == "setmode not supported on this platform"))result, mode = lfs.setmode(f, "text")assert((result and mode == "binary") or (not result and mode == "setmode not supported on this platform"))f:close()end– Restore access time to current valueassert (lfs.touch (tmpfile, attrib.access, attrib.modification))new_att = assert (lfs.attributes (tmpfile))assert (new_att.access == attrib.access)assert (new_att.modification == attrib.modification)– Remove new file and directoryassert (os.remove (tmpfile), "could not remove new file")assert (lfs.rmdir (tmpdir), "could not remove new directory")assert (lfs.mkdir (tmpdir..sep.."lfs_tmp_dir") == nil, "could create a directory inside a non-existent one")– Trying to get attributes of a non-existent fileassert (lfs.attributes ("this couldn*t be an actual file") == nil, "could get attributes of a non-existent file")assert (type(lfs.attributes (upper)) == "table", "couldn*t get attributes of upper directory")– Stressing directory iteratorcount = 0for i = 1, 4000 dofor file in lfs.dir (tmp) docount = count + 1endendprint"Ok!"

❾ linux平台:使用lua语言遍历某一文件夹下所有文件

你可以参考如下实例代码:functiongetFile(file_name)localf=assert(io.open(file_name,'r'))localstring=f:read("*all")f:close()returnstringendfunctionwriteFile(file_name,string)localf=assert(io.open(file_name,'w'))f:write(string)f:close()end–从命令行获取参数,如果有参数则遍历指定目录,没有参数遍历当前目录ifarg[1]~=nilthencmd="ls"..arg[1]elsecmd="ls"endprint("cmd",cmd)–io.popen返回的是一个FILE,跟c里面的popen一样locals=io.popen(cmd)localfileLists=s:read("*all")print(fileLists)whiletruedo–从文件列表里一行一行的获取文件名_,end_pos,line=string.find(fileLists,"([^]+.txt)",start_pos)ifnotend_posthenbreakend–print("wld",line)localstr=getFile(line)–把每一行的末尾1,替换为0,localnew=string.gsub(str,"1,","0,");–替换后的字符串写入到文件。以前的内容会清空writeFile(line,new)start_pos=end_pos+1end

未经允许不得转载:山九号 » lua获取文件名|linux平台:使用lua语言遍历某一文件夹下所有文件

赞 (0)