views:

43

answers:

3

this is my error code:

text = open('/media/a.txt', 'rb').read()

and my perplexed is:

when i use this :

text = open('a.txt', 'rb').read()

it can be running

but when i put the 'a.txt' to the 'media' folder, i can't running ,

why ?

thanks

IOError at /
[Errno 13] file not accessible: '/media/a.txt'
+1  A: 

The initial / means that it is an absolute path, accessed from the root of the filesystem. If you want to read a file from the project path then you will need to start the filename with the project path instead, or use __file__ along with the functions in os.path to create an appropriate relative or absolute path.

Ignacio Vazquez-Abrams
+2  A: 

To be more generic (and be sure that you use the media folder), you could change it to:

import os
text = open(os.path.join(settings.MEDIA_ROOT, 'a.txt', 'rb').read()
Gert
You'll also want to include `from django.conf import settings`, to make your example complete.
vezult
A: 

You cannot read static files from your application code in Google App Engine. Files marked as static are served from different servers and not included with your application. If your application needs to read them and they don't need to be served directly to users, don't mark them static. If you need to both serve them directly to users and read them in python code, you need to include 2 copies in your project, one of which is marked as static and one which isn't.

Wooble
I don't think the OP said anything about GAE.
vezult