views:

46

answers:

1

How to open a file in the parent directory in python in AppEngine?

I have a python file module/mod.py with the following code

f = open('../data.yml')
z = yaml.load(f)
f.close()

data.yml is in the parent dir of module. The error I get is

IOError: [Errno 13] file not accessible: '../data.yml'

I am using AppEngine SDK 1.3.3.

Is there a work around for this?

+2  A: 

The open function operates relative to the current process working directory, not the module it is called from. If the path must be module-relative, do this:

import os.path
f = open(os.path.dirname(__file__) + '/../data.yml')
Marcelo Cantos
Thanks It worked.
GeekTantra