views:

91

answers:

3

I used to open files that were in the same directory as the currently running Python script by simply using a command like open("Some file.txt", "r"). However, I discovered that when the script was run in Windows by double-clicking it, it would try to open the file from the wrong directory.

Since then I've used a command of the form
open(os.path.join(sys.path[0], "Some file.txt"), "r")
whenever I wanted to open a file. This works for my particular usage, but I'm not sure if sys.path[0] might fail in some other use case. So my question is: What is the best and most reliable way to open a file that's in the same directory as the currently running Python script?

Here's what I've been able to figure out so far:

  • os.getcwd() and os.path.abspath('') return the "current working directory", not the script directory.

  • os.path.dirname(sys.argv[0]) and os.path.dirname(__file__) return the path used to call the script, which may be relative or even blank (if the script is in the cwd). Also, __file__ does not exist when the script is run in IDLE or PythonWin.

  • sys.path[0] and os.path.abspath(os.path.dirname(sys.argv[0])) seem to return the script directory. I'm not sure if there's any difference between these two.

[edit:]

I just realized that what I want to do would be better described as "open a file in the same directory as the containing module". In other words, if I import a module I wrote that's in another directory, and that module opens a file, I want it to look for the file in the module's directory. I don't think anything I've found is able to do that...

+6  A: 

I always use:

__location__ = os.path.realpath(
    os.path.join(os.getcwd(), os.path.dirname(__file__)))

The join() call prepends the current working directory, but the documentation says that if some path is absolute, all other paths left of it are dropped. Therefore, getcwd() is dropped when dirname(__file__) returns an absolute path.

Also, the realpath call resolves symbolic links if any are found. This avoids troubles when deploying with setuptools on Linux systems (scripts are symlinked to /usr/bin/ -- at least on Debian).

You may the use the following to open up files in the same folder:

f = open(os.path.join(__location__, 'bundled-resource.jpg'));
# ...

I use this to bundle resources with several Django application on both Windows and Linux and it works like a charm!

André Caron
If `__file__` cannot be used, then use `sys.argv[0]` instead of `dirname(__file__)`. The rest should work as expected. I like using `__file__` because in library code, `sys.argv[0]` might not point to your code at all, expecially if imported via some 3rd party script.
André Caron
The problem with this is it will vary if you the file being run is from the interrupter directly or if it is imported. See my answer for the differences between __file__ and sys.argv[0]
Zimm3r
A: 

I'd do it this way:

from os.path import abspath, exists

f_path = abspath("fooabar.txt")

if exists(f_path):
    with open(f_path) as f:
        print f.read()

The above code builds an absolute path to the file using abspath and is equivalent to using normpath(join(os.getcwd(), path)) [that's from the pydocs]. It then checks if that file actually exists and then uses a context manager to open it so you don't have to remember to call close on the file handle. IMHO, doing it this way will save you a lot of pain in the long run.

dcolish
This does not answer the poster's question. dln385 specifically said that `os.path.abspath` does not resolve paths to files in the same folder as the script if the script is not in the current directory.
André Caron
AH! I assumed the user was running this script in the same dir as the file they wanted to read, *NOT* in the module dir of something in their PYTHONPATH. That'll teach me to make assumptions...
dcolish
A: 

Ok here is what I do

sys.argv is always what you type into the terminal or use as the file path when executing it with python.exe or pythonw.exe

For example you can run the file text.py several ways, they each give you a different answer they always give you the path that python was typed.

    C:\Documents and Settings\Admin>python test.py
    sys.argv[0]: test.py
    C:\Documents and Settings\Admin>python "C:\Documents and Settings\Admin\test.py"
    sys.argv[0]: C:\Documents and Settings\Admin\test.py

Ok so know you can get the file name, great big deal, now to get the application directory you can know use os.path, specifically abspath and dirname

    import sys, os
    print os.path.dirname(os.path.abspath(sys.argv[0]))

That will output this:

   C:\Documents and Settings\Admin\

it will always output this no matter if you type python test.py or python "C:\Documents and Settings\Admin\test.py"

The problem with using _file_ Consider these two files test.py

import sys
import os

def paths():
        print "__file__: %s" % __file__
        print "sys.argv: %s" % sys.argv[0]

        a_f = os.path.abspath(__file__)
        a_s = os.path.abspath(sys.argv[0])

        print "abs __file__: %s" % a_f
        print "abs sys.argv: %s" % a_s

if __name__ == "__main__":
    paths()

import_test.py

import test
import sys

test.paths()

print "--------"
print __file__
print sys.argv[0]

Output of "python test.py"

C:\Documents and Settings\David>python test.py
__file__: test.py
sys.argv: test.py
abs __file__: C:\Documents and Settings\David\test.py
abs sys.argv: C:\Documents and Settings\David\test.py

Output of "python test_import.py"

C:\Documents and Settings\David>python test_import.py
__file__: C:\Documents and Settings\David\test.pyc
sys.argv: test_import.py
abs __file__: C:\Documents and Settings\David\test.pyc
abs sys.argv: C:\Documents and Settings\David\test_import.py
--------
test_import.py
test_import.py

So as you can see file gives you always the python file it is being run from, where as sys.argv[0] gives you the file that you ran from the interpreter always. Depending on your needs you will need to choose which one best fits your needs.

Zimm3r
This is an elaborate proof that the implementation reflects the documentation. `__file__` is *supposed* to "always give you the path to the current file", and `sys.argv[0]` is *supposed* to "always give the path of the script that initiated the process". In any case, using `__file__` in the script that gets invoked always gives you precise results.
André Caron