4

My script.py creates a temporary file using a relative path.

When running it as:

python script.py

it works as expected.

But it doesn't work when you run it like:

python /path/to/script.py

The problem is that I don't know which path it will be running in. How can I get the absolute path to the script folder (the "/path/to") so the temporary file can be created in the same directory as the script?

What about the following?

os.path.abspath(os.path.dirname(__file__))
outis
  • 75,655
  • 22
  • 151
  • 221
Juanjo Conti
  • 28,823
  • 42
  • 111
  • 133
  • Does this answer your question? [How do you properly determine the current script directory?](https://stackoverflow.com/questions/3718657/how-do-you-properly-determine-the-current-script-directory) – outis Feb 04 '22 at 09:48

3 Answers3

12

Per the great Dive Into Python:

import sys, os

print 'sys.argv[0] =', sys.argv[0]             1
pathname = os.path.dirname(sys.argv[0])        2
print 'path =', pathname
print 'full path =', os.path.abspath(pathname)
Jonathan Feinberg
  • 44,698
  • 7
  • 80
  • 103
  • 1
    What about os.path.abspath(os.path.dirname(\_\_file__)) ? – Juanjo Conti Feb 13 '10 at 23:24
  • As per other [great answer](http://stackoverflow.com/a/1297407/1226722) which explains exactly why `__file__` is not what you're looking for: `__file__` is the path of the currently executing file (script or module). This is *accidentally* the same as the script if it is accessed from the script. – gregoltsov Oct 02 '14 at 11:11
  • @gregoltsov ```__file__``` does not work when you compile to .exe with pyinstaller – tiberhockey Dec 27 '21 at 23:33
9

The two current answers reflect the ambiguity of your question.

When you've run python /path/to/script.py, where do you want your tempfile? In the current directory (./tempfile.txt) or in /path/to/tempfile.txt?

If the former, you can simply use the relative path (or, for weird and arcane purposes, get the absolute path equivalent to the current directory as @Desintegr suggests, with os.getcwd).

If the latter, you can learn exactly how the script was invoked with sys.argv[0], as @Jonathan suggests, and manipulate that path with the functions in os.path (of course you can also apply those functions to what os.getcwd returns, if the former case applies), or work with os.path.dirname(__file__) and the like (the latter's necessary if you want this latter behavior also when the script is imported as a module, not just when it's run as a main script).

Alex Martelli
  • 854,459
  • 170
  • 1,222
  • 1,395
  • Thanks, I want the temp file in the same dir as the script.py – Juanjo Conti Feb 13 '10 at 23:44
  • 1
    @Juanjo, then `os.path.dirname(__file__)` is probably best and the answer you've selected second-best (as it requires one more `import` -- of `sys` -- which using `__file__` avoids). – Alex Martelli Feb 14 '10 at 00:15
0

You can use the os.getcwd() method to know the current working directory.

Return a string representing the current working directory.
Availability: Unix, Windows.

You can use the os.chdir(path) method to change the current working directory.

Change the current working directory to path.
Availability: Unix, Windows.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Desintegr
  • 6,992
  • 1
  • 22
  • 17