Question

i'm very new to lua scripting, i'm starting with create and write text to file by Lua with this script:

A = "Hello"
local file = io.open ('test.txt',"w")
file:write(A)
file:close()

And I got this error:

:3 Attempt to index local 'file' (a nil value)

What's wrong here?

P/s: I'm running this lua on camera with CHDK.

Was it helpful?

Solution

io.open will return nil if it was unable to open the file. You can retrieve the error message:

A = "Hello"
local file, err = io.open ('test.txt',"w")
if file==nil then
    print("Couldn't open file: "..err)
else
    file:write(A)
    file:close()
end

See: http://www.lua.org/pil/21.2.html

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top