2

I have myfile.txt sitting in the same directory as my lua file, yet when I call io.input("myfile.txt") I get the error bad argument #1 to 'input' (myfile.txt: No such file or directory).

I don't see how this can be going wrong, have I misunderstood Lua's I/O?

1 Answer 1

3

You need to fully qualify the path (eg. "/home/username/myfile.txt" on *NIX, "C:\directory\myfile.txt" on Windows). When you don't, the Lua interpreter expects the file to be in the same directory as the interpreter.

Read this question for an easy fix using arg[0] to get the current directory of the script being executed.

Sign up to request clarification or add additional context in comments.

3 Comments

Thank you! However, I'm executing this code use Love2d, which might put a bit of a spanner in the works regarding getting the current directory. Any suggestions?
If Love2d doesn't give you a way to load resources itself then I would hope it gives you a way to get your base directory location.
If you're using Love2d, I strongly suggest you use the API function, love.filesystem.read (love2d.org/wiki/love.filesystem.read)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.