1

I am writing a script to add one line in a .txt per video while using MPV.

However, I am getting a weird error on line 68 with the for loop. It merely tells me: no error. If I add an error parameter to file:write(message, error), it gives me another error message, stating: bad argument #2 to 'write' (string expected, got function). Any help would be appreciated.

function on_file_end(event)
    if not paused then totaltime = totaltime + os.clock() - lasttime end
    local message = (totaltime .. "s, " .. timeloaded .. ", " .. filename)
    local lines = {}
    local file = io.open(logpath, "r+")
    if file_exists(logpath) then
        for l in file:lines() do 
            if not l:find(message, 1, true) then
                lines[#lines+1] = 1
                file:write(message)
            end
        end
        file:close()
    end
end
Nifim
  • 4,758
  • 2
  • 12
  • 31
Kayi
  • 11
  • 3
  • please provide the full error message. why do you think it is an error if it says "no error"? which line is 68? `for l in file:lines() do` ? – Piglet Feb 09 '22 at 12:19
  • That's right. It does not say anything else. I have checked the log. – Kayi Feb 09 '22 at 12:39
  • again: why do you think it is an error when it says "no error"? – Piglet Feb 09 '22 at 14:25

1 Answers1

1

bad argument #2 to 'write' (string expected, got function)

error is not an "error parameter" it is a global function that allows to raise your own errors in Lua.

See https://www.lua.org/manual/5.4/manual.html#pdf-error

Piglet
  • 27,501
  • 3
  • 20
  • 43
  • I see, but how do I implement it in my script or fix this weird thing with "no error"? – Kayi Feb 09 '22 at 12:55
  • I don't understand why I am receiving this oddity. Would really appreciate some help. – Kayi Feb 09 '22 at 13:38
  • https://user-images.githubusercontent.com/28716224/153213914-20747a32-bd9f-45d3-97fb-9b9001d59d16.png. Here is the image with all the logs related to the script - temptimelogger. I hope its helpful – Kayi Feb 09 '22 at 13:48
  • I don't see any "no error" on this screenshot. it's just the error that I helped you with in your last question – Piglet Feb 09 '22 at 14:23
  • Sorry about that. I created a new StackOverflow post and would appreciate your input if you can see a solution for me now: https://stackoverflow.com/questions/71051860/my-code-to-write-to-a-file-does-not-work-file-remains-empty – Kayi Feb 09 '22 at 15:01