Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Read a file on App Engine with Python?

Is it possible to open a file on GAE just to read its contents and get the last modified tag?

I get a IOError: [Errno 13] file not accessible: I know that i cannot delete or update but i believe reading should be possible Has anyone faced a similar problem?

os.stat(f,'r').st_mtim 
like image 680
PanosJee Avatar asked Apr 13 '10 14:04

PanosJee


People also ask

How do I open a file with a specific app in Python?

Opening a file with subprocess. All you need to do to launch a program is call subprocess. call() and pass in a list of arguments where the first is the path to the program, and the rest are additional arguments that you want to supply to the program you're launching.

How do you read a file to Python?

To read a text file in Python, you follow these steps: First, open a text file for reading by using the open() function. Second, read text from the text file using the file read() , readline() , or readlines() method of the file object. Third, close the file using the file close() method.

Is Python used in App Engine?

App Engine offers you a choice between two Python language environments.


2 Answers

You've probably declared the file as static in app.yaml. Static files are not available to your application; if you need to serve them both as static files and read them as application files, you'll need to include 2 copies in your project (ideally using symlinks, so you don't actually have to maintain an actual copy.)

Update Nov 2014:

As suggested in the comments, you can now do this with the application_readable flag:

application_readable 

Optional. By default, files declared in static file handlers are uploaded as static data and are only served to end users, they cannot be read by an application. If this field is set to true, the files are also uploaded as code data so your application can read them. Both uploads are charged against your code and static data storage resource quotas.

See https://cloud.google.com/appengine/docs/python/config/appconfig#Static_Directory_Handlers

like image 61
Wooble Avatar answered Sep 19 '22 18:09

Wooble


You can read files, but they're on Goooogle's wacky GAE filesystem so you have to use a relative path. I just whipped up a quick app with a main.py file and test.txt in the same folder. Don't forget the 'e' on st_mtime.

import os from google.appengine.ext import webapp from google.appengine.ext.webapp import util   class MainHandler(webapp.RequestHandler):    def get(self):     path = os.path.join(os.path.split(__file__)[0], 'test.txt')      self.response.out.write(os.stat(path).st_mtime)   def main():   application = webapp.WSGIApplication([('/', MainHandler)],                                        debug=True)   util.run_wsgi_app(application)   if __name__ == '__main__':   main() 
like image 36
MStodd Avatar answered Sep 21 '22 18:09

MStodd