2014-08-29 94 views
0

相關代碼GAE的Python:閱讀JSON文件gitkit

嘗試1:

directory = os.path.dirname (__file__) 
path = os.path.join (directory, 'json', 'gitkit-server-config.json') 
gitkit_instance = gitkitclient.GitkitClient.FromConfigFile (path) 

嘗試2:

directory = os.path.dirname (__file__) 
path = os.path.join (directory, 'gitkit-server-config.json') 
gitkit_instance = gitkitclient.GitkitClient.FromConfigFile (path) 

獲取開發服務器上下面的錯誤,以及生產服務器: (兩次嘗試中都有類似錯誤)

INFO 2014-08-29 14:34:21,621 module.py:642] default: "GET /_ah/warmup HTTP/1.1" 500 - ERROR 2014-08-29 09:04:26,540 wsgi.py:262]

Traceback (most recent call last):

File "C:\Program Files (x86)\Google\google_appengine\google\appengine\runtime\wsgi.py", line 239, in Handle

handler = _config_handle.add_wsgi_middleware(self._LoadHandler()) 

File "C:\Program Files (x86)\Google\google_appengine\google\appengine\runtime\wsgi.py", line 298, in _LoadHandler

handler, path, err = LoadObject(self._handler) 

File "C:\Program Files (x86)\Google\google_appengine\google\appengine\runtime\wsgi.py", line 84, in LoadObject

obj = __import__(path[0]) 

File "C:\gaurav\coding\python\myapp\myapp\main_v3.py", line 107, in

gitkit_instance = gitkitclient.GitkitClient.FromConfigFile (path) 

File "C:\gaurav\coding\python\myapp\myapp\gitkitclient.py", line 193, in FromConfigFile

json_data = simplejson.load(open(config)) 

File "C:\Program Files (x86)\Google\google_appengine\google\appengine\tools\devappserver2\python\stubs.py", line 248, in init

raise IOError(errno.EACCES, 'file not accessible', filename) 

IOError: [Errno 13] file not accessible: 'C:\gaurav\coding\python\myapp\myapp\json\gitkit-server-config.json'

INFO 2014-08-29 14:34:26,591 module.py:642] default: "GET /_ah/warmup HTTP/1.1" 500 -

app.yaml中的相關片段:

:: 
handlers: 
# For Google Identity Toolkitv3 Oauth2 
- url: /gitkit-server-config\.json 
    static_files: gitkit-server-config.json 
    upload: gitkit-server-config\.json 
- url: /json 
    mime_type: application/json 
    static_dir: json 
:: 

文件(「gitkit服務器-config.json')被複制並保存在兩個地方:

C:\\gaurav\\coding\\python\\myapp\\myapp\\gitkit-server-config.json 
C:\\gaurav\\coding\\python\\myapp\\myapp\\json\\gitkit-server-config.json 

當我在網絡瀏覽器中輸入"http://www.myapp.com/gitkit-server-config.json",文件被下載。

我在做什麼錯?感謝你的幫助。

+1

當一個文件被聲明爲靜態的(應用程序:您可以配置「真application_readable」 .yaml),你看不懂它!您必須創建一個非靜態副本。 – voscausa 2014-08-29 14:01:07

+0

@voscausa非常感謝。問題已解決。我修改了app.yaml的相關部分,如下所示:' - url:/ json mime_type:application/json static_dir:json application_readable:true' 請提供您的建議作爲答案,我會接受它。 – gsinha 2014-08-29 14:16:15

回答

2

,如果你有閱讀使用Python文件IO這個文件不聲明靜態文件在你的app.yaml。做一個非靜態副本或更改您的app.yaml

更新:在你的app.yaml靜態目錄:

- url: /static 
    static_dir: static 
    application_readable: true 
+0

謝謝。問題已解決。我修改了app.yaml的相關部分,如下所示:' - url:/ json mime_type:application/json static_dir:json application_readable:true' – gsinha 2014-08-29 14:42:08

1

所有路徑都應該與app.yaml所在的位置有關。所以,如果你的目錄結構就像

| - myapp/ 
| | -app.yaml 
| | - json/ 
|  | - gitkit-server-config.json 

你可以試試:

path = os.path.join('json', 'gitkit-server-config.json') 
gitkit_instance = gitkitclient.GitkitClient.FromConfigFile (path) 
+1

感謝您的回覆。我用你的代碼替換了代碼,但是現在出現了類似下面的錯誤:'IOError:[Errno 13]文件無法訪問:'json \\ gitkit-server-config.json' – gsinha 2014-08-29 13:50:34

+1

+1試圖幫助我。謝謝 – gsinha 2014-08-29 14:17:09