views:

473

answers:

3

I tried to read a file in a view like this:

def foo(request):
    f = open('foo.txt', 'r')
    data = f.read()
    return HttpResponse(data)

I tried to place the foo.txt in almost every folder in the project but it still returns

[Errno 2] No such file or directory: 'foo.txt'

So does anybody knows how to open a file in app engine patch? Where should i place the files i wish to open? many thanks. I'm using app-engine-patch 1.1beta1

+1  A: 

In App Engine, patch or otherwise, you should be able to open (read-only) any file that gets uploaded with your app's sources. Is 'foo.txt' in the same directory as the py file? Does it get uploaded (what does your app.yaml say?)?

Alex Martelli
i tried to put the txt in the same directory as the py file but it didnt work. i think it is the app-engine-patch did something when opening file cause it has a lot of app engine things in the trackback.
lilo
This does not mean app-engine-patch has anything in common - this is the whole call stack that leads to this error. Paste your tracebask somewhere, we'll try to help (eg. at http://paste.pocoo.org/).
zgoda
Pasting the trace in the question would be better.
Nick Johnson
A: 

You should try f = open('./foo.txt', 'r')

ikutsin
A: 

Put './' in front of your file path:

f = open('./foo.txt')

If you don't, it will still work in App Engine Launcher 1.3.4, which could be confusing, but once you upload it, you'll get an error.

Also it seems that you shouldn't mention the file (or its dir) you want to access in app.yaml. I'm including css, js and html in my app this way.

Dan Stocker