2010-04-13 114 views
37

是否可以在GAE上打开文件来读取其内容并获取最后修改的标签?用Python读取App Engine上的文件?

我得到IO错误:[错误13]文件无法访问: 我知道,我不能删除或更新,但我相信读书应该可以 有没有人遇到过类似的问题呢?

os.stat(f,'r').st_mtim 
+0

尝试使用相对目录,并确保它在你的应用程序,而不是我已经根 – 2010-04-13 14:26:46

+0

已经尝试过, 当我有一个路径问题,我得到了otehr Errno 2 – PanosJee 2010-04-13 14:34:45

+0

Google员工回应并告诉我,你不能读取标记为静态的文件。但那是我的问题,因为我想打开我的JS/CSS文件。我会让你知道,如果我找到smth – PanosJee 2010-04-13 14:50:49

回答

52

您可能在app.yaml中声明了该文件为静态文件。静态文件不适用于您的应用程序;如果您需要将它们作为静态文件提供并作为应用程序文件读取,则需要在项目中包含2个副本(理想情况下使用符号链接,因此实际上不必保留实际副本。)

更新2014年11月:

正如意见提出,现在可以用application_readable标志做到这一点:

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.

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

+0

我如何在GAE环境中使用symblink? – PanosJee 2010-04-15 14:14:53

+7

您在本地副本中创建符号链接;这些文件实际上将被复制两次到App Engine:一次到静态文件服务器,一次到应用程序服务器。您还可以通过将本地副本符号链接到应用程序目录来包含外部程序包,而不是将整个程序包复制到每个使用它的项目。 – geoffspear 2010-04-15 17:31:26

+0

这是一个很棒的提示! thanx! thanx! – PanosJee 2010-04-16 15:46:18

11

您可以读取文件,但它们位于Goooogle古怪的GAE文件系统中,因此您必须使用相对路径。我刚刚在同一个文件夹中创建了一个main.py文件和test.txt的快速应用程序。不要忘记st_mtime上的'e'。

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() 
+7

除了无法使用静态文件外,应用程序的文件访问没有什么特别之处。 – 2010-04-14 10:36:51

+0

这是否适用于webapp2? – Jonny 2013-09-02 09:32:25

6

为新的“application_readable:true”功能+1。在使用这个新功能之前,我遇到了GAE的“古怪的”文件系统的问题,同时让NLP Montylingua导入。

问题:Monty使用open(filename,'rb')和一个指向file_ptr.read()的文件指针,以字节为单位从静态文件中获取。我的实现工作在我的本地Windows系统上,但部署失败!

的修复:指定预期的要读取的字节file_ptr.read(4)#4的二进制字节

显示被什么东西相关的64位GAE服务器想要读入更多(8默认)字节。无论如何,花了一段时间找到这个问题。 Montylingua现在加载。

1

我来到了陌生的,但工作的解决方案:) :)神社

提供静态文件直接有时成为GAE头疼。性能可能的权衡让你直接与金嘉移动

- url: /posts/(.*\.(md|mdown|markdown)) 
    mime_type: text/plain 
    static_files: static/posts/\1 
    upload: posts/(.*\.(md|mdown|markdown)) 



from jinja2 import Environment 
from jinja2.loaders import FileSystemLoader 
posts = Environment(loader=FileSystemLoader('static/posts/')) # Note that we use static_files folder defined in app.yaml 
post = posts.get_template('2013-11-13.markdown') 

import markdown2 # Does not need of course 

class Main(webapp2.RequestHandler): 

    def get (self): 
     self.response.headers[ 'Content-Type' ] = 'text/html' 

     self.response.write (markdown2.markdown(post.render())) # Jinja + Markdown Render function 

你有没有得到它;)我测试了它,它的工作。

1

随着webapp2的,假设你在相同的路径main.pypages/index.html

#!/usr/bin/env python 

import webapp2, os 

class MainHandler(webapp2.RequestHandler): 
    def get(self): 
     path = os.path.join(os.path.split(__file__)[0], 'pages/index.html') 
     with open(path, 'r') as f: 
      page_content = f.read() 
     self.response.write(page_content) 

app = webapp2.WSGIApplication([ 
    ('/', MainHandler) 
], debug=True)