copy csv file to new file in lua

copy, file, lua

Solution

There are many ways to do this.

If the file is small enough, you can read the whole thing into a string, and write the string to the other file:

infile = io.open("temp.csv", "r")
instr = infile:read("*a")
infile:close()

outfile = io.open("log.csv", "w")
outfile:write(instr)
outfile:close()

You can also invoke your shell to do the copy, though that is platform specific:

os.execute("cp temp.csv log.csv")

Problem

Can you copy a file in lua? Is this possible? You would probably think just insert "devices" into new file however, that creates a new string in each loop -a loop that I did not include in this snippet. ``` file = io.open("temp.csv", "a") file:write(devices) file = io.open("log.csv", "w") file:write("") if (count = 15) then --copy "temp.csv" to "log.csv" end ```

Original source