1

I have encountered a problem while trying to open a .txt file from the same directory where my source code is located. When I tried to open the file like this:

with open("pi_digits.txt") as file_object:
        contents = file_object.read()
print(contents)

I failed. I also failed when I typed the whole path:

with open("Users\lukas\Documents\python_work\chapter_10") as file_object:
        contents = file_object.read()
print(contents)

But when I typed:

with open("\\Users\\lukas\\Documents\\python_work\\chapter_10\\pi_digits.txt") as file_object:
    contents = file_object.read()
print(contents)

I succeeded!

So my question is: Why can't I run the code without error when I enter the following code:

with open('pi_digits.txt') as file_object:
    contents = file_object.read()
print(contents)

Thank you for your answers and sorry if my question was not well constructed.

Spottvogel
  • 13
  • 3
  • Paths are relative to your current working directory, not the path to the script. – jordanm Jul 17 '22 at 14:13
  • Does this answer your question? [How do I get the path of the Python script I am running in?](https://stackoverflow.com/questions/595305/how-do-i-get-the-path-of-the-python-script-i-am-running-in) – jordanm Jul 17 '22 at 14:13
  • Where is the `pi_digits.txt` file located? Using the filename only, unless the file is in the same directory as the Python script, how can the file be located? – S3DEV Jul 17 '22 at 14:13
  • Windows absolute path doesn't look like that, they are `"C:\\Users\\XXX\\Document"` – azro Jul 17 '22 at 14:15
  • You will need to do a little investigation of escape characters in python strings. For instance, `"hello\tworld"` puts a tab between hello and world, and `"hello\nworld"` puts a newline between hello and world. Since a backslash is used for this kind of escaping in strings, you must be careful with them in file paths. Usually we use two backslashes to escape the backslashes (which means that \\ is interpreted as \ - a simple backslash with no special meaning anymore. – topsail Jul 17 '22 at 14:24

3 Answers3

0

the thing with paths is python is that they are referenced from where you launch the program (current working directory), not from where the file lives. So, I recommend making use of the os and pathlib packages to manage file paths properly.

Why can't I run the code without error when I enter the following code:

It depends from where you execute your python code.

Sala
  • 480
  • 4
  • 19
0

@Sottvogel

In general, a python instance can be run from many different directories in your computer. One way to check where the running instance is:

import os 
os.getcwd()

If you confirm that you are in the same directory of your file, try and see what the following returns:

os.listdir()

In case your file doesn't appear in the returned string, then there has to be another problem. Make sure you checkout the docs as well.

Hope it helps!

nandevers
  • 191
  • 8
0

Building on above, where you execute your code implies wether you need to use the file name or it's path.

If you execute your Python code in the same directory as the txt file, then you can use the file's name. However, if it is located elsewhere the script will need to access the file and hence, requires the path to it.