无法在使用python的OpenShift中使用json打开文件
我对Openshift中的python代码有问题 我的应用程序上有一个子域,我必须在其中打开一个json格式的txt文件无法在使用python的OpenShift中使用json打开文件,python,json,openshift,Python,Json,Openshift,我对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'
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()
中删除前两行,应用程序就可以完美运行。我尝试从命令行运行flask localy,它在那里也可以运行。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
应用程序根目录:
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
我发现了问题
import os
json_data = open(os.path.join(os.path.dirname(__file__),"cs1.txt"),'r')
而不是
json_data = open("cs1.txt")
你能发布你的
cs1.txt
文件的内容吗?你的获取有错误吗?@Vor我添加了错误。在日志控制台上,我有一个简单的错误500。没别的了。对于文件,是一种有效的json格式。我已经使用过很多次了,特别是如果我在本地运行应用程序,它不会有任何问题。这不是很有用,请尝试在调试模式下运行Flask
app.debug=True
,或者将这两行放在try-except-block和log-an中error@Vor更新了更多错误