2016-12-06 129 views
0

我搜索了這個錯誤,但找不到如何處理它。我收到以下錯誤,試圖打開一個文件時:Python Error [Errno 36]:文件名太長

[錯誤36]太長文件名:「在/ var/WWW/FlaskApp/FlaskApp /模板/

這裏是我的簡單的代碼。我試圖打開一個JSON文件,並使用Flask將其渲染到網站中:

@app.route("/showjson/") 
def showjson(): 
    SITE_ROOT = os.path.realpath(os.path.dirname(__file__)) 
    data_in = open(os.path.join(SITE_ROOT, "static/data", "btc.json"), "r") 
    data_out = "" 
    for line in data_in: 
     data_out += line.rstrip() 
    data_in.close() 
    return render_template(data_out) 

有沒有人知道解決方案?提前謝謝了。

+0

你可以拉出'os.path.join(SITE_ROOT, 「靜態/數據」, 「btc.json」)'並打印它返回的內容? – TemporalWolf

回答

1

當您正在查找模板文件的文件名時,您正在將render_template函數傳遞給您的整個JSON文件。這就是爲什麼你得到一個文件名太長的錯誤。可以使用send_from_directory函數發送JSON文件。首先導入功能:

from flask import send_from_directory 

然後使用它像這樣:

@app.route("/showjson/") 
def showjson(path): 
    SITE_ROOT = os.path.realpath(os.path.dirname(__file__)) 
    return send_from_directory(os.path.join(SITE_ROOT, "static/data"), "btc.json") 
+0

Thx它的工作原理!但是,我將如何返回一個不是來自目錄文件的巨大字符串? – saitam

相關問題