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

gad*_*dss 12 size lua file coronasdk

我在使用Lua获取文件大小时遇到​​问题.我正在创建一个函数方法,如果文件的文件大小是743 bytes,那么该文件将被删除.

这是我的代码:

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),
            }

            -- i don't know what is the syntax
            if myFile.size == 743 bytes then
                 myFile.delete
            end             

end
Run Code Online (Sandbox Code Playgroud)

任何人都可以帮我解决我的情况吗?

lhf*_*lhf 23

大小由myFile:seek("end").给出.

要删除该文件,请使用os.remove(path).但请先关闭文件.

  • @gadss,似乎您在搜索之前关闭文件,而您应该在*之后*搜索之后关闭文件。 (2认同)

Sat*_*hJM 8

最近Lua文件系统支持被添加到Corona!您可以使用获取文件大小

local lfs = require "lfs"
local size = lfs.attributes (path, "size")
Run Code Online (Sandbox Code Playgroud)

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

删除文件使用

os.remove(path)
Run Code Online (Sandbox Code Playgroud)