Search code examples
macosfile-ioluafile-permissionslove2d

Lua file handling error: Permission Denied (Mac OSX Yosemite)


I'm struggling with a permission error in Lua when trying to read/write from/to a text file. As you can see below, I've pulled the error message from the io.open function and I'm getting "file.txt: permission denied". If it helps at all, I'm using Mac OSX Yosemite and the Love2D engine.

function fileWrite()
    outputFile, error = io.open("new.txt", "w")
    if outputFile then
        for k,v in pairs(clicks) do
            outputFile:write(tostring(v[1]) .. "," .. tostring(v[2]) .. "\n")
        end
        outputFile:close()
    else
        errorText = error
    end
end

Am I making a silly error somewhere by any chance? I've dealt with writing to files in Lua before (on Windows 7), and I've never had this problem before.

Any feedback would be greatly appreciated! :)


Solution

  • In LÖVE your game shouldn't be interacting directly with the file system through io. Instead uselove.filesystem.newFile so your assets will still be available within a .love (zip) file. This should also handle the permission issues your having on OS X as it will write to /Users/user/Library/Application Support/LOVE/ to which love will have write permissions.

    function fileWrite()
        outputFile, error = love.filesystem.newFile("new.txt")
        if outputFile:open("w") then
            outputFile:write("Hello World!")
            outputFile:close()
        else
            print(error)
        end
    end