首页 > 解决方案 > Lua 读取文件并写入新文件

问题描述

我需要复制一个文件并将扩展名从 .seq 更改为 mid (不使用 shell 命令)这有效

file = io.open(source_filename, "rb")
source_content = file:read("*all")
file = io.open(source_filename ..".mid", "wb")
file:write(source_content)
file:close()

我明白了Song.seq.mid ,但我想Song seq.mid

如果我做一个

source_filename = string.gsub(source_filename, ".seq", ".mid")
file = io.open(source_filename, "wb")

然后文件有一个零值file:write(source_content)

标签: filelua

解决方案


您可以source_filename在打开文件进行写入之前进行修改:source_filename = source_filename:gsub("seq$", "mid"). 这将seq在文件名的末尾替换为mid,达到预期的效果。


推荐阅读