【问题标题】:Structure of files in flask pythonanywhere server?烧瓶pythonanywhere服务器中的文件结构?
【发布时间】:2017-05-26 21:59:57
【问题描述】:

我开始学习如何在 pythonanywhere 中工作,所以我遇到了一些问题...... 我的网络应用具有以下结构:

    /home/mentor/mysite/servidor/ here I've run.py and the folder app

    inside app there's init.py , views.py and the folders: static,scripts,templates

我的问题是网络内部有一个表单,当有人点击按钮时,views.py 中会调用脚本中的一个函数。该函数需要读取一个 .csv 文件(我已将该文件保存在 .../servidor/ 中)。

但网页没有运行,它返回 500 Internal Server Error 因为 OSError: File b'Names.csv' 不存在。 为什么我能解决这个问题?我需要把那个文件放在哪里?是run.py还是WSGI配置文件的问题?

谢谢!

PD.:代码

在 viwes.py 中:

 from .scripts.file import function

 @app.route('/func', methods=['POST'])
 def resp():
     l=[request.form['d1'].....]
     f=function(l)
     .....

在脚本文件夹中的 file.py 中:

  import pandas as pd
  def function(l):
      df=pd.read_csv('Names.csv') #Here is the problem!
      .....

【问题讨论】:

  • 能否包含访问文件的代码?
  • @Luis Orduz ,我刚写下来,可以吗?或者你还想看吗?

标签: csv web-applications flask structure pythonanywhere


【解决方案1】:

您可以利用 __file__ 打开与 Python 模块相关的文件。

import os


def open_here(filename, encoding='UTF-8'):
    """Open a file in the same directory as this module."""
    here = os.path.dirname(os.path.abspath(__file__))
    return open(os.path.join(here, filename), encoding=encoding)


# example snippet
def foo():
    with open_here('my_file') as f:

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-11-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多