【问题标题】:Open a text file on a python web application using flask使用烧瓶在 python Web 应用程序上打开一个文本文件
【发布时间】:2013-05-30 11:12:51
【问题描述】:

我有一个分析文本数据的 Python 应用程序,目的是找到文本的主题。

我需要做的是让这个应用程序成为一个网络应用程序。

我将 apache、mod_wsgi 和 flask 框架设置在一起,并且我正确地制作了一个简单的应用程序,它表明一切正常。

但是当我尝试调用 open() 函数时,我得到内部服务器错误。 我确信路径是正确的。我在一个独立的应用程序上试了一下,没有任何问题。

你有什么线索吗?

编辑: 我现在尝试的代码与我的应用程序无关。

这是主要的烧瓶文件

    #!/usr/bin/python

    from flask import Flask,request,render_template


    import T_testing

    app = Flask(__name__)


    @app.route('/pref',methods = ['GET','POST'])  
    def pref():
        error = None
        return render_template('layout.html',error = error)

    @app.route('/running', methods = ['GET','POST'])
    def running ():
      error = None

      if request.method == 'POST':  
        dat = request.form['dat']
        alg = request.form['alg']
        dist = request.form['dist']
        threshold = request.form['threshold']
        numoc = request.form['numoc']


        string = T_testing.run_function(dat,alg,dist,threshold,numoc)
        return string


    if __name__ == '__main__':
        app.config['TRAP_BAD_REQUEST_ERRORS'] = True
        app.run(host='0.0.0.0',port=5000,debug=True)

这是 T_testing.py

def run_function(st1,st2,st3,st4,st5):
        f = open('/home/christos/public/apps/flaskt/txt_files/textdata.txt','r')
        f.close()
        tablist = [st1,st2,st3,st4,st5]
        strlist = []
        for i in range( 0,len(tablist),1):
            strlist.append(str(tablist[i]))
        return "Choises ARE: "+strlist[0]+" AND "+strlist[1]+" AND "+strlist[2]+" AND "+strlist[3]+" AND "+strlist[4]+" !!!!"

(不要评判代码,我只是测试..) 如您所见,我从 layout.html 中获得了一些首选项,但现在我只是打印它们。

没有 T_testing 中关于 open() 和 close() 的 2 行,没有问题。顺便说一句,我使用 Ubuntu。正如我所说,该路径正在一个独立的 python 应用程序上运行。

编辑: 好的,所以我对我的问题没有任何解决方案。 有人在烧瓶 Web 应用程序中使用过 open() 函数吗? 我想知道我是否应该继续尝试或寻找另一种方法来制作我的网络应用程序..

编辑: 我还尝试使用 open('file-name.txt','w') 创建一个新文件,以检查路径是否错误,但再次出现错误...

【问题讨论】:

  • 显示代码和回溯。路径不太可能是正确的。

标签: python web-applications file-io flask


【解决方案1】:

运行 Apache 的用户很可能没有读取文本文件的权限 - 您需要 chmod 相关文件以确保它是可读的。

$ chmod o+r /path/to/your/file

【讨论】:

  • 你好肖恩,谢谢你的回答。我用 o+r 和 777 尝试了 chmod,但还是什么都没有……而且 error.log 没有显示任何内容……
【解决方案2】:

尝试将您的textdata.txt 文件复制到/tmp 目录,确认它具有全局读取权限,然后更改您的打开路径:

  f = open('/tmp/textdata.txt','r')

如果成功,那么您可能在原始路径中的某个地方遇到了权限问题。

【讨论】:

    猜你喜欢
    • 2018-12-15
    • 2018-09-03
    • 1970-01-01
    • 1970-01-01
    • 2016-04-12
    • 2019-09-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多