IOError: File file.csv does not exist. Flask Python

IOError: File file.csv does not exist. Flask Python

这是我的代码:

import os
import pandas as pd
from flask import Flask, request,render_template, redirect, url_for, send_from_directory
from werkzeug.utils import secure_filename





# create app
app = Flask(__name__)

app.config['UPLOAD_FOLDER'] = '/home/Firiyuu77/mysite/uploads'
app.config['ALLOWED_EXTENSIONS'] = set(['txt','csv','xlsx'])

def allowed_file(filename):
    return '.' in filename and \
           filename.rsplit('.', 1)[1] in app.config['ALLOWED_EXTENSIONS']


@app.route('/')
def main():
    return render_template('index.html')

# Route that will process the file upload
@app.route('/upload', methods=['POST'])
def upload():
    # Get the name of the uploaded file
    file = request.files['file']
    # Check if the file is one of the allowed types/extensions
    if file and allowed_file(file.filename):
        # Make the filename safe, remove unsupported chars
        filename = secure_filename(file.filename)
        # Move the file form the temporal folder to
        # the upload folder we setup
        file.save(os.path.join(app.config['UPLOAD_FOLDER'], filename))
        # Redirect the user to the uploaded_file route, which
        # will basicaly show on the browser the uploaded file
        return redirect(url_for('uploaded_file',
                                filename=filename))

# This route is expecting a parameter containing the name
# of a file. Then it will locate that file on the upload
# directory and show it on the browser, so if the user uploads
# an csv , that csv  is going to be returned after the eupload then evaluation.
@app.route('/uploads/<filename>', methods=['GET', 'POST'])
def uploaded_file(filename):
    if __name__ == '__main__':
      app.run(
      host="0.0.0.0",
      port=int("80"),
      debug=True
         )

    if request.method == 'GET':
        # show html form
       return render_template('formcsv.html')
    elif request.method == 'POST':
        # calculate result
       data_df = pd.read_csv(filename)
       data_df['Forecasted Values:']=0


       m1 = int(request.form.get('m1'))
       m2 = int(request.form.get('m2'))


       for i, row in data_df.iterrows() :
          rem = data_df.iloc[i]['Current SOH']
          sold1 = data_df.iloc[i][m1]
          sold2 = data_df.iloc[i][m2]

          rem = int(rem)
          sold1 = int(sold1)
          sold2 = int(sold2)
          result = forecast(rem,sold1,sold2)

          data_df.set_value([i], ['Forecasted Values:'], result)

       data_df.to_csv(filename)


       return send_from_directory(app.config['UPLOAD_FOLDER'],filename)

它实际上是一个获取 csv 上传的应用程序,通过 pandas 和 returns 将其处理给用户。但是不知何故,当我得到这个回溯时,我被困在这里,控制台上没有错误,只是在网站错误日志上。

2017-04-18 15:33:59,759 :Traceback (most recent call last):
2017-04-18 15:33:59,760 :  File "/home/Firiyuu77/.virtualenvs/my-virtualenv/local/lib/python2.7/site-packages/flask/app.py", line 1982, in wsgi_app
2017-04-18 15:33:59,760 :    response = self.full_dispatch_request()
2017-04-18 15:33:59,760 :  File "/home/Firiyuu77/.virtualenvs/my-virtualenv/local/lib/python2.7/site-packages/flask/app.py", line 1614, in full_dispatch_request
2017-04-18 15:33:59,760 :    rv = self.handle_user_exception(e)
2017-04-18 15:33:59,760 :  File "/home/Firiyuu77/.virtualenvs/my-virtualenv/local/lib/python2.7/site-packages/flask/app.py", line 1517, in handle_user_exception
2017-04-18 15:33:59,760 :    reraise(exc_type, exc_value, tb)
2017-04-18 15:33:59,760 :  File "/home/Firiyuu77/.virtualenvs/my-virtualenv/local/lib/python2.7/site-packages/flask/app.py", line 1612, in full_dispatch_request
2017-04-18 15:33:59,760 :    rv = self.dispatch_request()
2017-04-18 15:33:59,760 :  File "/home/Firiyuu77/.virtualenvs/my-virtualenv/local/lib/python2.7/site-packages/flask/app.py", line 1598, in dispatch_request
2017-04-18 15:33:59,760 :    return self.view_functions[rule.endpoint](**req.view_args)
2017-04-18 15:33:59,760 :  File "/home/Firiyuu77/mysite/flask_app.py", line 61, in uploaded_file
2017-04-18 15:33:59,760 :    data_df = pd.read_csv(filename)
2017-04-18 15:33:59,760 :  File "/home/Firiyuu77/.virtualenvs/my-virtualenv/local/lib/python2.7/site-packages/pandas/io/parsers.py", line 646, in parser_f
2017-04-18 15:33:59,760 :    return _read(filepath_or_buffer, kwds)
2017-04-18 15:33:59,761 :  File "/home/Firiyuu77/.virtualenvs/my-virtualenv/local/lib/python2.7/site-packages/pandas/io/parsers.py", line 389, in _read
2017-04-18 15:33:59,761 :    parser = TextFileReader(filepath_or_buffer, **kwds)
2017-04-18 15:33:59,761 :  File "/home/Firiyuu77/.virtualenvs/my-virtualenv/local/lib/python2.7/site-packages/pandas/io/parsers.py", line 730, in __init__
2017-04-18 15:33:59,761 :    self._make_engine(self.engine)
2017-04-18 15:33:59,761 :  File "/home/Firiyuu77/.virtualenvs/my-virtualenv/local/lib/python2.7/site-packages/pandas/io/parsers.py", line 923, in _make_engine
2017-04-18 15:33:59,761 :    self._engine = CParserWrapper(self.f, **self.options)
2017-04-18 15:33:59,761 :  File "/home/Firiyuu77/.virtualenvs/my-virtualenv/local/lib/python2.7/site-packages/pandas/io/parsers.py", line 1390, in __init__
2017-04-18 15:33:59,761 :    self._reader = _parser.TextReader(src, **kwds)
2017-04-18 15:33:59,761 :  File "pandas/parser.pyx", line 373, in pandas.parser.TextReader.__cinit__ (pandas/parser.c:4184)
2017-04-18 15:33:59,761 :  File "pandas/parser.pyx", line 667, in pandas.parser.TextReader._setup_parser_source (pandas/parser.c:8449)
2017-04-18 15:33:59,761 :IOError: File p2c-inventory-performance-20170219173023.csv does not exist

为什么在回溯中指出文件的正确名称时文件不存在?所以它应该读过它。为什么找不到它的任何想法?我在上传文件夹及其那里搜索了它,但尚未对其进行评估,这意味着从读取文件名时未找到它。我应该如何校准它才能使这项工作有任何想法?我搜索了一些解决方案,但它们是针对带有指定文件名的文件,它们只是向其中添加了 "r",但在我的身上,我正在为其使用变量 -> filename 但是当我添加 "r" 通过 "r" + filename 它仍然不起作用,很抱歉成为这样一个笨蛋,但我真的在努力解决这个问题。需要帮助

您使用的是相对路径,工作目录与您预期的不同。使用绝对路径或确保相对路径位于正确的工作目录中。