简体   繁体   English

无法在python的OpenShift中使用json打开文件

[英]Cannot open a file with json in OpenShift with python

I have a problem with python code in Openshift. 我在Openshift中遇到python代码问题。

I have a subdomain on my app where I have to open a txt file with json format. 我的应用程序上有一个子域,我必须在其中打开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")

if I remove the first two lines from cs1() the app works perfect. 如果我从cs1()删除前两行,则该应用程序会完美运行。 I tried to run flask localy from command line and it works there as well. 我试图从命令行运行flask localy,它也在那里正常工作。 The cs1.txt file is in the same root with the main.py. cs1.txt文件与main.py位于同一根目录中。

Error: Internal Server Error 错误:内部服务器错误

The server encountered an internal error and was unable to complete your request. 服务器遇到内部错误,无法完成您的请求。 Either the server is overloaded or there is an error in the application. 服务器超载或应用程序中有错误。

Error on log: 日志错误:

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

Application root: 应用程序根目录:

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

I found the problem. 我发现了问题。

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

instead of 代替

json_data = open("cs1.txt")

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM