2010-04-13 115 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)