No such file or directory

Questions about the LÖVE API, installing LÖVE and other support related questions go here.
Forum rules
Before you make a thread asking for help, read this.
Post Reply
kolino1750
Prole
Posts: 7
Joined: Wed Aug 19, 2020 8:22 pm

No such file or directory

Post 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
User avatar
ivan
Party member
Posts: 1915
Joined: Fri Mar 07, 2008 1:39 pm
Contact:

Re: No such file or directory

Post 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.
kolino1750
Prole
Posts: 7
Joined: Wed Aug 19, 2020 8:22 pm

Re: No such file or directory

Post 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
User avatar
ivan
Party member
Posts: 1915
Joined: Fri Mar 07, 2008 1:39 pm
Contact:

Re: No such file or directory

Post 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.
kolino1750
Prole
Posts: 7
Joined: Wed Aug 19, 2020 8:22 pm

Re: No such file or directory

Post 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
User avatar
ivan
Party member
Posts: 1915
Joined: Fri Mar 07, 2008 1:39 pm
Contact:

Re: No such file or directory

Post 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.
kolino1750
Prole
Posts: 7
Joined: Wed Aug 19, 2020 8:22 pm

Re: No such file or directory

Post 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
User avatar
AuahDark
Party member
Posts: 107
Joined: Mon Oct 23, 2017 2:34 pm
Location: Indonesia
Contact:

Re: No such file or directory

Post 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.
Profile. Do you encounter crashes in LÖVE Android and wanna send me logcats? Please hit me up in LÖVE Discord and send the full logcat file!
kolino1750
Prole
Posts: 7
Joined: Wed Aug 19, 2020 8:22 pm

Re: No such file or directory

Post 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
kolino1750
Prole
Posts: 7
Joined: Wed Aug 19, 2020 8:22 pm

Re: No such file or directory

Post 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
Post Reply

Who is online

Users browsing this forum: Ahrefs [Bot], Bing [Bot], Google [Bot] and 1 guest