Take the 2-minute tour ×

Working on OS X Lion, I'm trying to open a file in my python-program from anywhere in the terminal. I have set the following function in my .bash_profile:

function testprogram() {python ~/.folder/.testprogram.py}

This way I can(in the terminal) run my testprogram from a different directory than my ~/.

Now, if I'm in my home directory, and run the program, the following would work

infile = open("folder2/test.txt", "r+")

However, if I'm in a different directory from my home-folder and write "testprogram" in the terminal, the program starts but is unable to find the file test.txt.

Is there any way to always have python open the file from the same location unaffected of where i run the program from?

share|improve this question
What does the testprogram() function have to do with the Python program? –  David Robinson Aug 29 '12 at 18:24
add comment

6 Answers

up vote 2 down vote accepted

If you want to make it multiplatform I would recommend

import os
share|improve this answer
Thanks, this worked very well! –  Noorac Aug 29 '12 at 18:34
add comment

Use the tilde to represent the home folder, just as you would in the .bash_profile, and use os.path.expanduser.

import os
infile = open(os.path.expanduser("~/folder2/test.txt"), "r+")
share|improve this answer
Did you test that? Doesn't seem to work for me. –  Joachim Isaksson Aug 29 '12 at 18:25
@JoachimIsaksson: Sorry, forgot to add os.path.expanduser. –  David Robinson Aug 29 '12 at 18:30
add comment

This is no different than referring to a file via anything else besides Python, you need to use an absolute path rather than a relative one.

If you'd like to refer to files relative to the location of the script, you can also use the __file__ attribute in your module to get the location of the currently running module.

Also, rather than using a shell function, just give your script a shebang line (#!/usr/bin/env python), chmod +x it, and put it someplace on your PATH.

share|improve this answer
I use the env method described here regularly. Depending how you use custom modules, you may need to update your PYTHONPATH as well. –  DrSkippy Aug 29 '12 at 18:29
add comment

You could simply run the function in a subshell so you can cd home before running it:

function testprogram() { (
    cd && python .folder/.testprogram.py
) }
share|improve this answer
add comment

Use either full path like


or abbreviated one

share|improve this answer
add comment

In python sys.argv[0] will be set to the path of the script (if known). You can use this plus the functions in os.path to access files relative to the directory containing the script. For example

import sys, os.path

script_path = sys.argv[0]
script_dir = os.path.dirname(script_path)

def script_relative(filename):
  return os.path.join(script_dir, filename)

infile = open(script_relative("folder2/test.txt"), "r+")

David Robinson points out that sys.argv[0] may not be a full pathname. Instead of using sys.argv[0] you can use __file__ if this is the case.

share|improve this answer
sys.argv[0] isn't guaranteed to give the path from the current directory to the script. For example, the script could just be on the user's path. –  David Robinson Aug 29 '12 at 18:32
@DavidRobinson, huh, pydoc sys implies that it is always a path name, but docs.python.org/library/sys.html says it might not be. So I suggested to use __file__ instead. –  Geoff Reedy Aug 29 '12 at 18:40
add comment

Your Answer


