Python file loading question [SOLVED]

hello, when I load a file using open(filename,‘r’).readLine() it tries to load the file from C:\users\hobbit. is there a way for me to load files like models and actors–starting from execution directory-- or is there a python way to solve this?

docs.python.org/library/os.html#os-file-dir should help here

especially os.getcwd() and os.chdir()

“/path/to/file” is “C:\path\to\file”
“path/to/file” is “C:… your program dir …\path\to\file”

normally i create folder data where everything is stored. So all my files are
“data\path\to\file”

I am not sure how I would use os.chdir() if i still need to find out what directory I am in. Right now os.getcwd() returns like ‘C:\users\hobbit’ instead of where the program is launched from, which is
‘C:\users\hobbit\Desktop\Game’. So how would I find the directory that the .py file is launched in without a hard link?

os.path.dirname(file)
What’s your sys.path[0] ?

So if I understand correctly you are trying to find out the directory that the script you are executing is running from, which is possibly different from the current working directory?

In that case I believe the previous poster is correct. In order for a running script to be able to execute other scripts in the same directory, even if it was called from elsewhere Python prepends sys.path with the either the location of the script or ‘’ if the cwd and the script location are the same.

So if you run:

import sys
print sys.path[0]

It should display the location your script is running from (or ‘’).

-Scott.

thanks ynjh_jo and scottgr, that solved my problem