大多数Lua'ish方式做到这一点,但也许不是最有效的:
-- load the ltn12 module
local ltn12 = require("ltn12")
-- copy a file
ltn12.pump.all(
ltn12.source.file(assert(io.open("temp.csv", "rb"))),
ltn12.sink.file(assert(io.open("log.csv", "wb")))
)
而且在这之前,你需要确保你有LuaSocket,一个简单的环境:
sudo luarocks install luasocket
甚至还有更好的办法:
==== util.lua ====
-- aliases for protected environments
local assert, io_open
= assert, io.open
-- load the ltn12 module
local ltn12 = require("ltn12")
-- No more global accesses after this point
if _VERSION == "Lua 5.2" then _ENV = nil end
-- copy a file
local copy_file = function(path_src, path_dst)
ltn12.pump.all(
ltn12.source.file(assert(io_open(path_src, "rb"))),
ltn12.sink.file(assert(io_open(path_dst, "wb")))
)
end
return {
copy_file = copy_file;
}
===== main.lua ====
local copy_file = require("util").copy_file
copy_file("temp.csv", "log.csv")
谢谢你,那是一个好主意 – kevintdiy 2013-05-03 22:35:10