Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

lua

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.

like image 841
Ben Mack Avatar asked May 08 '14 02:05

Ben Mack


1 Answers

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

like image 191
Odoth Avatar answered Oct 31 '22 14:10

Odoth