这里有一个答案-File not found on pythonanywhere.com
但是对于我而言,它不起作用。
我已将文件放在项目主目录和--registrar:static
目录中-
我的代码-
/project/static
我也尝试过-
from flask import Flask, render_template, request
from flask_cors import CORS, cross_origin
from diffdiag import DifferentialDiagScript2 as dd
import os
app = Flask(__name__)
app.config['CORS_HEADERS'] = 'Content-Type'
cors = CORS(app, resources={r"/*": {"origins": "*"}})
@app.route('/')
@cross_origin()
def index():
module_dir = os.path.dirname(__file__)
file_path = os.path.join(module_dir, 'literature.csv')
items = list(line.strip() for line in open(file_path))
return render_template('index.html', table = items)
似乎没有任何作用。
感谢您的帮助。
编辑1-添加完整错误
items = list(line.strip() for line in open(f2))
items = list(line.strip() for line in open('./static/f2'))
items = list(line.strip() for line in open('/home/daddyodevil/add/Automated_DD/f2'))
items = list(line.strip() for line in open('/home/daddyodevil/add/Automated_DD/static/f2'))
编辑2-添加尝试在pythonanywhere中运行Exception on / [GET]#012Traceback (most recent call last):#012 File "/home/daddyodevil/.local/lib/python3.6/site-packages/flask/app.py", line 2292, in wsgi_app#012 response = self.full_dispatch_request()#012 File "/home/daddyodevil/.local/lib/python3.6/site-packages/flask/app.py", line 1815, in full_dispatch_request#012 rv = self.handle_user_exception(e)#012 File "/home/daddyodevil/.local/lib/python3.6/site-packages/flask_cors/extension.py", line 161, in wrapped_function#012 return cors_after_request(app.make_response(f(*args, **kwargs)))#012 File "/home/daddyodevil/.local/lib/python3.6/site-packages/flask/app.py", line 1718, in handle_user_exception#012 reraise(exc_type, exc_value, tb)#012 File "/home/daddyodevil/.local/lib/python3.6/site-packages/flask/_compat.py", line 35, in reraise#012 raise value#012 File "/home/daddyodevil/.local/lib/python3.6/site-packages/flask/app.py", line 1813, in full_dispatch_request#012 rv = self.dispatch_request()#012 File "/home/daddyodevil/.local/lib/python3.6/site-packages/flask/app.py", line 1799, in dispatch_request#012 return self.view_functions[rule.endpoint](**req.view_args)#012 File "/home/daddyodevil/.local/lib/python3.6/site-packages/flask_cors/decorator.py", line 128, in wrapped_function#012 resp = make_response(f(*args, **kwargs))#012 File "/home/daddyodevil/add/Automated_DD/app.py", line 13, in index#012 items = list(line.strip() for line in open("./static/f2"))#012FileNotFoundError: [Errno 2] No such file or directory: './static/f2'
时收到的错误
app.py
答案 0 :(得分:2)
在PythonAnywhere控制台中运行app.py不太可能起作用-它将尝试绑定到端口并监听连接,并且其他人完全有可能正在使用该端口。
您在网站错误日志中看到的问题是因为您试图打开文件'./static/f2'
。可以相对于应用程序运行所在的目录来解析此路径,该目录不一定与包含模块的目录相同,并且也可能不是主目录。
如果您要将其加载到与模块相同的目录中,并且已经有代码可以像这样计算模块目录
module_dir = os.path.abspath(os.path.dirname(__file__))
...然后您可以通过更改行来更改代码以使用该module_dir
items = list(line.strip() for line in open("./static/f2"))
...要这样:
items = list(line.strip() for line in open(os.path.join(module_dir, "./static/f2")))
答案 1 :(得分:0)
链接的答案对您不起作用,因为您没有正确地遵循它。将模块目录包装在os.path.abspath()
中,以获取链接答案所建议的绝对路径:
module_dir = os.path.abspath(os.path.dirname(__file__))
修改
还要确保此文件相对于您的模块目录确实存在:
'./static/f2'
因为这是引发异常的地方(请查看堆栈跟踪的最后一行):
items = list(line.strip() for line in open("./static/f2"))
#012FileNotFoundError: [Errno 2] No such file or directory: './static/f2'