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?

georgep
  • 23
  • 3

1 Answers1

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.

Community
  • 1
  • 1
SolarBear
  • 4,534
  • 4
  • 37
  • 53
  • 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? – georgep Dec 27 '13 at 16:09
  • 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. – Etan Reisner Dec 27 '13 at 16:18
  • 3
    If you're using Love2d, I strongly suggest you use the API function, `love.filesystem.read` (https://www.love2d.org/wiki/love.filesystem.read) – SolarBear Dec 27 '13 at 16:25