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