如何在Lua中获取文件大小并删除文件?

我在使用Lua获取文件大小方面遇到了问题。我正在创建一个函数方法,如果文件的文件大小为“743个字节”,则将删除该文件。

这是我的代码:

local getDLFile = function(fileToDL)
            local path = system.pathForFile(fileToDL, system.DocumentsDirectory )
            local myFile = io.open( path, "w+b" )
            http.request{
                url = "http://www.testfile.com/"..fileToDL,
                sink = ltn12.sink.file(myFile),
            }

            -- 我不知道语法是什么
            if myFile.size == 743 bytes then
                 myFile.delete
            end

end

有人能帮我解决这个问题吗?

点赞
用户107090
用户107090

文件大小可以通过 myFile:seek("end") 来获取。

若要删除文件,可以使用 os.remove(path)。但请先关闭文件。

2012-05-23 03:07:44
用户1137788
用户1137788

最近,Lua文件系统支持被添加到了Corona!你可以使用以下方式获取文件大小:

local lfs = require "lfs"
local size = lfs.attributes (path, "size")

你可以在这里阅读更多信息 http://keplerproject.github.com/luafilesystem/manual.html#reference

要删除文件,请使用以下代码:

os.remove(path)
2012-05-23 03:34:46