Page 1 of 1

No such file or directory

Posted: Sun Sep 06, 2020 3:56 pm
by kolino1750
I am using the following function to get the content of a text file:

Code: Select all

function readAll(file)
    local f = assert(io.open(file, "rb"))
    local content = f:read("*all")
    f:close()
    return content
end
I dont know how to put the correct path to my text file. I always get "No such file or directory" error...

For example: I want to open a text file on my desktop. Is this the correct path?

Code: Select all

 C:\\Users\\John\\Desktop\\data.txt

Re: No such file or directory

Posted: Sun Sep 06, 2020 4:24 pm
by ivan
\\ is a windows thing you may want to use / instead to ensure your script works across platforms.
./ would allow you to use paths relative to the current working directory.
"rb" means that you are reading a binary file. with "txt" files you can just use "r"
Other than that the code looks fine.

Re: No such file or directory

Posted: Sun Sep 06, 2020 4:43 pm
by kolino1750
ivan wrote: Sun Sep 06, 2020 4:24 pm \\ is a windows thing you may want to use / instead to ensure your script works across platforms.
./ would allow you to use paths relative to the current working directory.
"rb" means that you are reading a binary file. with "txt" files you can just use "r"
Other than that the code looks fine.
I still get "No such file or directory" when using / instead of \\.

Is this the correct way to use the relative path to the current working directory?

Code: Select all

text = readAll('./data.txt')
Image

Re: No such file or directory

Posted: Sun Sep 06, 2020 4:56 pm
by ivan
Maybe it's a permissions thing - Windows blocks certain directories if your account is not an administrator. That's why it's better to use love.filesystem - it's guaranteed to work across platforms.
The following works fine for me:

Code: Select all

local file = io.open("./license.txt", "r")
local cont = file:read("*all")
print(cont)
Just remember that ./ is usually relative to the love.exe binaries, not your game folder.

Re: No such file or directory

Posted: Sun Sep 06, 2020 5:29 pm
by kolino1750
ivan wrote: Sun Sep 06, 2020 4:56 pm Maybe it's a permissions thing - Windows blocks certain directories if your account is not an administrator. That's why it's better to use love.filesystem - it's guaranteed to work across platforms.
The following works fine for me:

Code: Select all

local file = io.open("./license.txt", "r")
local cont = file:read("*all")
print(cont)
Just remember that ./ is usually relative to the love.exe binaries, not your game folder.
My working directory is this one: C:/Program Files/LOVE

I am now using this function:

Code: Select all

function readAll(filepath)
	for line in love.filesystem.lines(filepath) do
		local str = ""
		str = str + line
	end
	return str
end

text = readAll('./data.txt')
I placed my text file also in the working directory:

Image

I am still getting the error:

Image

Re: No such file or directory

Posted: Sun Sep 06, 2020 8:40 pm
by ivan
love.filesystem does not allow access outside of the AppData directory and your game folder.
That's why love.filesystem is "safer" to use than the default io. module.
Windows won't allow you to mess with the C:/Program Files/ folder unless you are an administrator.

Re: No such file or directory

Posted: Sun Sep 06, 2020 10:39 pm
by kolino1750
I am getting the error even as administrator. I dont know what to do anymore :shock:

Can you help me on discord? I could start a screen transmission

Re: No such file or directory

Posted: Mon Sep 07, 2020 4:19 am
by AuahDark
The short is:

1. Using "./" means relative path, which relative to current working directory (usually where love.exe reside)
2. You shouldn't mix Lua "io" and LOVE "love.filesystem". Use Lua "io" if you want to open file anywhere (which leads to portability issue) and use LOVE "love.filesystem" if you want access files in your game.

Re: No such file or directory

Posted: Mon Sep 07, 2020 12:50 pm
by kolino1750
AuahDark wrote: Mon Sep 07, 2020 4:19 am The short is:

1. Using "./" means relative path, which relative to current working directory (usually where love.exe reside)
2. You shouldn't mix Lua "io" and LOVE "love.filesystem". Use Lua "io" if you want to open file anywhere (which leads to portability issue) and use LOVE "love.filesystem" if you want access files in your game.
It always shows me "Could not open file.... Does not exist." :cry:

I placed my text file in the current working directory^^ I have added u on discord ^^ Can you help me please? I can start a screen transfer

Re: No such file or directory

Posted: Tue Sep 08, 2020 2:07 am
by kolino1750
Problem solved :awesome:

I forgot to turn on file extensions^^ I named my file data.txt but the extension .txt was not necessary...

So the correct path name was: data.txt.txt