1

I am trying to get the path where the script is currently executed. I found a lot of answers regarding this question, so my question is what's the best solution or even what the differences between all these are:

  • os.getcwd()
  • __file__
  • os.path.dirname(__file__)
  • os.path.dirname(os.path.realpath(__file__))
  • os.path.dirname(os.path.abspath( __file__ ))
  • os.path.dirname(sys.argv[0])
  • os.path.abspath(os.path.dirname(sys.argv[0]))

Especially I want to know, what's the best way of getting the path when the script is put into an .exe file, for example if I want to access files, which are in the same directory as the .exe.

Thanks in advance!

EDIT 1: I am working on windows.

Luca Tatas
  • 160
  • 1
  • 14

1 Answers1

1

Let me explain to you by explaining the outputs of all the commands you mentioned by running them in Windows.

Nothing special here, just importing some modules. Importing numpy to test the commands.

In [1]: import os
   ...: import sys
   ...: import numpy

This gives the current working directory in which your process/program is executing. (detailed tutorial here)

In [2]: os.getcwd()
Out[2]: 'C:\\Users\\<username>'

This tells which file the module is loaded from which file.

In [3]: numpy.__file__
Out[3]: 'C:\\Users\\<username>\\anaconda3\\lib\\site-packages\\numpy\\__init__.py'

The path of the loaded file for the module, it can be a relative path.

In [4]: os.path.dirname(numpy.__file__)
Out[4]: 'C:\\Users\\Punit Singh\\anaconda3\\lib\\site-packages\\numpy'

The actual path of the loaded file for the module. See the difference from previous in lib and Lib. The folder name actually starts from the capital letter.

In [5]: os.path.dirname(os.path.realpath(numpy.__file__))
Out[5]: 'C:\\Users\\Punit Singh\\anaconda3\\Lib\\site-packages\\numpy'

This is the absolute path, you'll see the difference when you'll run this in Linux. It starts with the root folder denoted by \.

In [6]: os.path.dirname(os.path.abspath(numpy.__file__))
Out[6]: 'C:\\Users\\Punit Singh\\anaconda3\\lib\\site-packages\\numpy'

[7] and [8] respectively give the path and absolute path of the file which is currently executing, here sys.argv[0] returns the file which is currently executing, since currently, I am running ipython, it is ipython.exe shown in [9]. (detailed tutorial here)

In [7]: os.path.dirname(sys.argv[0])
Out[7]: 'C:\\Users\\<username>\\anaconda3\\Scripts'

In [8]: os.path.abspath(os.path.dirname(sys.argv[0]))
Out[8]: 'C:\\Users\\<username>\\anaconda3\\Scripts'

In [9]: sys.argv[0]
Out[9]: 'C:\\Users\\<username>\\anaconda3\\Scripts\\ipython'

This gives the absolute path of the executable binary for the Python interpreter (documentation here)

In [10]: sys.executable
Out[10]: 'C:\\Users\\Punit Singh\\anaconda3\\python.exe'

Now coming to your requirement, I understand that you need to access files where this ipython.exe as shown in [9] is located, so you can use commands mentioned in [7] or [8].

thepunitsingh
  • 713
  • 1
  • 12
  • 30
  • Thank you for your brilliant answer! I have been using ```sys.argv[0]``` for a while, what do you think about ```sys.executable```? – Luca Tatas Dec 19 '20 at 20:57
  • I've updated the answer with `sys.executable` and also corrected a mistake I made in explaining `sys.argv[0]`, please re-read that section. – thepunitsingh Dec 19 '20 at 21:19