Attempting to index local "file" (nil value) in file: write

I am very new to lua scripting, I start by creating and writing text to a Lua file using 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' (nil value)

What is wrong here?

P / s: I run this lua on camera with CHDK.

+4
source share
1 answer

io.open will return nil if it could not open the file. You may receive an 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

+8
source

Source: https://habr.com/ru/post/1539686/


All Articles