Cannot find file using pathlib

Hi there, thanks in advance for trying to help!
I’m currently learning Python using Eric Matthes’ “Python Crash Course”. I’m stuck at trying to read a file using pathlib.
Here’s the code in question:

from pathlib import Path

path = Path('pi_digits.txt')
contents = path.read_text()
print(contents)

I then get the following error message:

PS C:\Users\aleks\Desktop\python_work> & C:/Users/aleks/AppData/Local/Programs/Python/Python311/python.exe "c:/Users/aleks/Desktop/python_work/Chapter 10/file_reader.py"
Traceback (most recent call last):
  File "c:\Users\aleks\Desktop\python_work\Chapter 10\file_reader.py", line 4, in <module>
    contents = path.read_text()
               ^^^^^^^^^^^^^^^^
  File "C:\Users\aleks\AppData\Local\Programs\Python\Python311\Lib\pathlib.py", line 1058, in read_text
    with self.open(mode='r', encoding=encoding, errors=errors) as f:
         ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\Users\aleks\AppData\Local\Programs\Python\Python311\Lib\pathlib.py", line 1044, in open
    return io.open(self, mode, buffering, encoding, errors, newline)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
FileNotFoundError: [Errno 2] No such file or directory: 'pi_digits.txt'

I have already tried using Bing chatbot to help me find the problem and doing that ruled out the following possibilities:

  1. No spelling mistakes
  2. Code should work
  3. The file_reader.py and pi_digits.txt files are in the same directory
  4. The pi_digits.txt file is not hidden or has restricted access
  5. Currently installed Python version is Python 3.11.2
  6. Updated pip to the latest version 23.0.1 and used the command pip install pathlib to install pathlib should it not be there already

After that Bing ran out of ideas and directed me towards this forum. I hope somebody can help me, since I cannot continue programming if I can’t read files :smiley:

From this you are can see that the pi_digits.txt file is not in the current directory.
I know this becuase there is no directory in the name of the file.

You can confirm this by using dir that will show your python script and will not list the pi_digits.txt file.

If you want to open the from some other directory you will need to include that in the filename.

Hi Barry, thanks for your suggestion!
However, the files are in fact in the same directory as you can see here:

PS C:\Users\aleks\Desktop\python_work> dir chapter_10


    Verzeichnis: C:\Users\aleks\Desktop\python_work\chapter_10


Mode                 LastWriteTime         Length Name
----                 -------------         ------ ----
-a----        08.04.2023     18:47            102 file_reader.py
-a----        08.04.2023     18:29             40 pi_digits.txt
-a----        08.04.2023     19:09        1030000 pi_million_digits.txt

EDIT:
I tried copying the file into the superordinate folder python_work and now the script works! However, when I type dir, I can see the pi_digits.txt file but not the file_reader.py file which is located in the Chapter_10 folder.

PS C:\Users\aleks\Desktop\python_work> dir


    Verzeichnis: C:\Users\aleks\Desktop\python_work


Mode                 LastWriteTime         Length Name
----                 -------------         ------ ----
d-----        17.03.2023     09:55                Chapter 5
d-----        21.03.2023     21:25                Chapter 6
d-----        27.03.2023     16:56                Chapter 7
d-----        01.04.2023     18:44                Chapter 8
d-----        08.04.2023     18:31                Chapter_10
d-----        27.03.2023     16:44                Chapter_3
d-----        16.03.2023     21:02                Chapter_4
d-----        07.04.2023     21:57                Chapter_9
-a----        11.03.2023     19:01             97 famous_quote.py
-a----        11.03.2023     19:03            151 famous_quote_2.py
-a----        11.03.2023     21:44            128 file_extenstions.py
-a----        17.03.2023     09:24            111 hello_world.py
-a----        11.03.2023     18:59             81 name_cases.py
-a----        11.03.2023     18:57            329 personal_message.py
-a----        08.04.2023     18:29             40 pi_digits.txt
-a----        11.03.2023     19:05             86 stripping_names.py

Do you have suggestions how I can fix that?

The parent of a process determines its initial working directory. It’s either inherited from the parent’s current working directory or set explicitly by the parent. In this case, the Python process is spawned by PowerShell with the initial working directory “C:\Users\aleks\Desktop\python_work”. In the script, Path('pi_digits.txt') tries to resolve the relative path “py_digits.txt” as a file in the current working directory.

The directory that has the “file_reader.py” script has nothing to do with resolving a relative file path, unless you manually change the current directory to that path, e.g. via os.chdir("C:/Users/aleks/Desktop/python_work/Chapter 10").

2 Likes

Hi Eryk, thank you very much for explaining that to me! I learned a lot about processes because of your answer.

This is not what matters. A path like this is a relative path, which is relative to the current working directory of the Python process, not necessarily the source code file.

Canonical on Stack Overflow:

Relevant supporting information:

2 Likes

Hi Karl, thank you very much for your reply and for the links, I will certainly check them out!

1 Like