1

I have a python script on another drive:

E:/myscripts/run.py

I am running this from cmd like this:

python

Once I am in the python interpreter, I just do this:

file = "E:/myscripts/run.py"
exec(open(file).read())

It throws an error "No such file or directory: config.txt". This is a file the script opens inside:

open("config.txt")

os.getcwd()

returns C:/Users/me

So it's trying to find the config file there. How can I tell python to look for the related files relative to where the executing script is?

Do I have to specifically change the current directory? Because if I do:

os.chdir("E:/myscripts")

then run my script again, it works. But I was hoping to have some arguments to run the script properly from where it resides.

Joan Venge
  • 315,713
  • 212
  • 479
  • 689
  • "Do I have to specifically change the current directory?" Yes you do, although you can avoid hardcoding the directory name by parsing the path name of the file: `os.chdir(os.path.dirname(file))` – blhsing Oct 31 '22 at 08:41
  • Thanks a lot. Because I am trying to help people who are not very technical to run the script, so this all makes it a bit harder than just drag the script on cmd window to run. – Joan Venge Oct 31 '22 at 08:46
  • If you are allowed to change the script itself, you should really avoid `open("config.txt")` in the first place and always open the file with its full path name. – blhsing Oct 31 '22 at 08:47
  • @blhsing: Well that's even worse. Because even though I can change the script, I don't know where the user will run the script from. The script could be in any directory. – Joan Venge Oct 31 '22 at 09:02
  • Does this answer your question? [How do I get the full path of the current file's directory?](https://stackoverflow.com/questions/3430372/how-do-i-get-the-full-path-of-the-current-files-directory) PS: The directory separator on Windows is ``\`` and not `/` as on Linux/Mac as explained by the Microsoft documentation about [Naming Files, Paths, and Namespaces](https://learn.microsoft.com/en-us/windows/win32/fileio/naming-a-file). – Mofi Oct 31 '22 at 10:31
  • The Python modules [pathlib](https://docs.python.org/3/library/pathlib.html) and [os.path](https://docs.python.org/3/library/os.path.html) have both functions to get a directory or file path with the native directory separator of the operating system being recommended to use at least on printing an information for a user with a directory or file name with path. – Mofi Oct 31 '22 at 10:41

1 Answers1

1

In your run.py script, you could first use the OS package to determine the current path of your run.py script and thus load config.txt. To determine the full path of your currently running script you can use the following command:

import os
path2runpy = os.path.dirname(__file__)

If you print the variable path2runpy with the print command the following path should appear:

E:/myscripts/

Then you can concatenate the path plus the filename and load the file:

open(os.path.join(path2runpy, "config.txt"))

os.getcwd() will only give you the current directory, from where you are calling the script.

TechnicTom
  • 66
  • 1
  • 4