1

I have a python script that reads some input from a file that is supposed to be in the same directory of the script:

with open('./input1.txt') as file:
    F = file.readlines()
Do_Stuff(F)

I usually run the script from other directories (not the directory the script and input files reside), so it raises an IOError that it can't find the input file (as it is not in my current working directory). I don't prefer putting a full path for the input file since I want it to be portable- only both the script and input files have to be in the same directory.

Is there a way to indicate the script to look in its location directory not the directory I am launching it from?

amyassin
  • 2,714
  • 3
  • 25
  • 32
  • 1
    Get the absolute path of the python file which is being run and use to to open your file! – theAlse Mar 18 '13 at 10:35
  • Use the full path for the file input1.txt, will work from wherever you launch this script. If ful-path is user-configurable, put it n a separate config file and then read it. Since you try to open ('./input1.txt') the OS will always look in the current directory – RedBaron Mar 18 '13 at 11:41
  • related: [How do I get the path of the current executed file in python?](http://stackoverflow.com/questions/2632199/how-do-i-get-the-path-of-the-current-executed-file-in-python) – jfs Mar 18 '13 at 12:47

2 Answers2

3

__file__ is the path of the script, albeit often a relative path. Use:

import os.path

scriptdir = os.path.dirname(os.path.abspath(__file__))

to create an absolute path to the directory, then use os.path.join() to open your file:

with open(os.path.join(scriptdir, './input1.txt')) as openfile:
Martijn Pieters
  • 1,048,767
  • 296
  • 4,058
  • 3,343
  • It still returns the path where I'm in, not where the script is... – amyassin Mar 18 '13 at 11:26
  • @amyassin: How are you running the script? `python path/to/script.py` will set `__file__` to `path/to/script.py` (e.g. a relative path). The above code *will* turn that into `/full/path/to`, an absolute path. Print `__file__` when you run your script, from various different command lines (local dir, another dir, etc.) to see what changes. I've used this many times before and it does *not* just use the current working directory. – Martijn Pieters Mar 18 '13 at 11:43
  • according to http://docs.python.org/2/library/os.path.html#os.path.abspath, it is equivalent to `normpath(join(os.getcwd(), path))` which uses the current working directory... – amyassin Mar 18 '13 at 11:47
  • @amyassin: `os.path.abspath()` will use `os.getcwd()` to base relative paths off, yes, but that doesn't make it equivalent. `normpath(join(os.getcwd(), '../../foo/bar/baz'))` is not going to be the same path as `os.getcwd()`. – Martijn Pieters Mar 18 '13 at 11:49
  • Well, I've tested it right now. Got `os.path.dirname(os.path.abspath('__file__'))` returning the cwd of where I execute it, not where it is.. – amyassin Mar 18 '13 at 11:53
  • @amyassin: and what does `print __file__` give you? – Martijn Pieters Mar 18 '13 at 11:54
  • Ok, the problem was that I have to do `os.path.abspath(__file__)` not `os.path.abspath('__file__')` – amyassin Mar 18 '13 at 11:55
  • 1
    @amyassin: ah, oops, that is indeed my mistake. Sorry about that! – Martijn Pieters Mar 18 '13 at 11:56
0

If setuptools/distribute are installed; you could use pkg_resources functions to access files:

import pkg_resources

data = pkg_resources.resource_string(__name__, 'input1.txt')
jfs
  • 399,953
  • 195
  • 994
  • 1,670