jagttt
jagttt

Reputation: 1070

How to open a file on app engine patch?

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

Upvotes: 0

Views: 2263

Answers (3)

Dan Stocker
Dan Stocker

Reputation: 702

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.

Upvotes: 1

ikutsin
ikutsin

Reputation: 1148

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

Upvotes: 0

Alex Martelli
Alex Martelli

Reputation: 882181

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?)?

Upvotes: 2

Related Questions