Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

FileNotFoundError: [Errno 2] No such file or directory: ''

133 views
Skip to first unread message

Quentin Bock

unread,
Apr 14, 2021, 12:14:07 PM4/14/21
to
I receive this error when I try to open a file
The file (what I'm trying to open) is in the same folder as the program I'm
trying to open it from; why is it saying no such file or directory?

this is the only part of the code that causes the error

file = open('Egils Saga 1-15.txt', "r")

file.close()

Python

unread,
Apr 14, 2021, 1:34:06 PM4/14/21
to
Cannot be reproduced, your file is definitely not where you think
it is:

$ ls *txt
'Egils Saga 1-15.txt'
$ cat t.py
file = open('Egils Saga 1-15.txt', "r")

file.close()

mac:/tmp$ python t.py
mac:/tmp$

jak

unread,
Apr 14, 2021, 1:35:54 PM4/14/21
to
>>> file.read()
'Hello from Egils Saga 1-15.txt'
>>> file.close()

Try to check your 'current working directory':

>>> import os
>>> os.getcwd()
'D:\\tmp' # where the file is
>>>

Dan Stromberg

unread,
Apr 14, 2021, 1:35:55 PM4/14/21
to
On Wed, Apr 14, 2021 at 9:14 AM Quentin Bock <qber...@gmail.com> wrote:

> I receive this error when I try to open a file
> The file (what I'm trying to open) is in the same folder as the program I'm
> trying to open it from; why is it saying no such file or directory?
>
> this is the only part of the code that causes the error
>
> file = open('Egils Saga 1-15.txt', "r")
>
> file.close()
>

Python will try to open the file in the directory you are currently in,
rather than the directory your program is in.

Try cd'ing to the appropriate directory first. Or specify a full to your
file in the open.

Dan Stromberg

unread,
Apr 14, 2021, 1:56:09 PM4/14/21
to
Open a cmd.exe, command.exe or powershell, and:

cd c:\my\dir\ect\ory
Then run your script.

Or put an os.chdir(r'c:\my\dir\ect\ory') at the top of your script.

Or use file = open(r'c:\my\dir\ect\ory\Egils Saga 1-15.txt', 'r')

BTW, "file" means something to python other than just a variable name. You
can replace it, as your code below (and my example above) does, but it
obscures the thing you're replacing. That's why I like to use file_
instead of file.

On Wed, Apr 14, 2021 at 10:42 AM Quentin Bock <qber...@gmail.com> wrote:

> Okay, how exactly do I do that?
> Thanks

Michael F. Stemper

unread,
Apr 14, 2021, 3:38:15 PM4/14/21
to
On 14/04/2021 12.55, Dan Stromberg wrote:
> Open a cmd.exe, command.exe or powershell, and:
>
> cd c:\my\dir\ect\ory
> Then run your script.
>
> Or put an os.chdir(r'c:\my\dir\ect\ory') at the top of your script.
>
> Or use file = open(r'c:\my\dir\ect\ory\Egils Saga 1-15.txt', 'r')
>
> BTW, "file" means something to python other than just a variable name. You
> can replace it, as your code below (and my example above) does, but it
> obscures the thing you're replacing. That's why I like to use file_
> instead of file.

Personally, I'd use a meaningful name. For instance, I might do

saga = open(r'c:\my\dir\ect\ory\Egils Saga 1-15.txt', 'r')

or even

with open(r'c:\my\dir\ect\ory\Egils Saga 1-15.txt', 'r') as saga:

The latter form eliminates the need for saga.close(). (I'm sure that you
know that; it's directed at the OP.)

--
Michael F. Stemper
The FAQ for rec.arts.sf.written is at
<http://leepers.us/evelyn/faqs/sf-written.htm>
Please read it before posting.

Eryk Sun

unread,
Apr 15, 2021, 12:40:35 AM4/15/21
to
On 4/14/21, Quentin Bock <qber...@gmail.com> wrote:
>
> this is the only part of the code that causes the error
>
> file = open('Egils Saga 1-15.txt', "r")

Here's an app_abspath() function to resolve a filename against the
directory of the main script:

import os
import sys

def get_main_file():
if hasattr(sys, 'frozen'):
return sys.executable
main = getattr(sys.modules.get('__main__'), '__file__', '')
return os.path.abspath(main) if main else ''

def get_main_dir():
return os.path.dirname(get_main_file()) or os.getcwd()

def app_abspath(filename):
return os.path.join(get_main_dir(), filename)

file = open(app_abspath('Egils Saga 1-15.txt'), 'r')

In the frozen script case, sys.executable is the main 'script'. For a
"-c" command, there is no main file, so it uses the current working
directory.

Using the variable name "file" is fine so long as compatibility with
Python 2 isn't required. In Python 3, "file" is not a reserved keyword
and not the name of a builtin function or type.
0 new messages