zoukankan      html  css  js  c++  java
  • lua遍历文件

    看了不少人的,主要还是错误处理有点问题,不多说了

    贴代码:

    require "lfs"
    
    
    function getpathes(rootpath, pathes)
        pathes = pathes or {}
    
        ret, files, iter = pcall(lfs.dir, rootpath)
        if ret == false then
            return pathes
        end
        for entry in files, iter do
            local next = false
            if entry ~= '.' and entry ~= '..' then
                local path = rootpath .. '/' .. entry
                local attr = lfs.attributes(path)
                if attr == nil then
                    next = true
                end
    
                if next == false then 
                    if attr.mode == 'directory' then
                        getpathes(path, pathes)
                    else
                        table.insert(pathes, path)
                    end
                end
            end
            next = false
        end
        return pathes
    end
    
    pathes = {}
    
    getpathes("/", pathes)
    
    for key, path in pairs(pathes) do
        print(key .. " " .. path)
    end
  • 相关阅读:
    Mayan游戏
    选择客栈
    Redundant Paths
    中心选址
    辗转相除
    字符串
    线段覆盖
    配置魔药
    宝库通道
    教官的监视
  • 原文地址:https://www.cnblogs.com/zelos/p/3826225.html
Copyright © 2011-2022 走看看