【问题标题】:Cannot open a file with json in OpenShift with python无法在 OpenShift 中使用 python 打开带有 json 的文件
【发布时间】:2013-11-05 16:17:53
【问题描述】:

Openshift 中的 python 代码有问题。

我的应用上有一个子域,我必须在其中打开一个 json 格式的 txt 文件。

from flask import Flask
from flask import render_template
import json

app = Flask(__name__)

@app.route("/")
def index():
    return render_template("home.html")


@app.route('/casestudy1')
def cs1():
    json_data = open("cs1.txt")
    data = json.load(json_data)
    ....do my staff....
    return render_template("cs1.html")

如果我从cs1() 中删除前两行,则该应用程序运行良好。我尝试从命令行本地运行烧瓶,它也可以在那里工作。 cs1.txt 文件与 main.py 位于同一根目录中。

错误:内部服务器错误

服务器遇到内部错误,无法完成您的请求。要么是服务器超载,要么是应用程序出错。

日志错误:

json_data = open("cs1.txt")
IOERROR: [Errno2] No such file or directory: cs1.txt

应用根目录:

-wsgi
  -static
     -css
     -js
  -templates
     -cs1.html
  -main.py
  -cs1.txt

【问题讨论】:

  • 您能否发布您的cs1.txt 文件的内容,但获取时出错?
  • @Vor 我添加了错误。在日志控制台上,我有一个简单的错误 500。仅此而已。至于文件,是有效的json格式。我已经使用过很多次了,尤其是如果我在本地运行应用程序,它没有任何问题。
  • 这不是很有用,尝试在调试模式下运行Flask app.debug = True 或将这两行放在try except block 中并记录错误
  • @Vor 更新了更多错误

标签: python json openshift


【解决方案1】:

我发现了问题。

import os
json_data = open(os.path.join(os.path.dirname(__file__),"cs1.txt"),'r')

而不是

json_data = open("cs1.txt")

【讨论】:

    猜你喜欢
    • 2017-12-03
    • 1970-01-01
    • 2021-11-04
    • 1970-01-01
    • 2011-06-20
    • 2018-04-01
    • 1970-01-01
    • 2016-01-15
    • 2015-04-02
    相关资源
    最近更新 更多