这是我的代码:
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操纵它并将其返回给用户。但不知怎的,当我得到这个追溯时,我被卡在这里,控制台上没有错误,只是在网站错误日志上。
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
为什么文件在回溯时指出文件的正确名称时不存在?所以它应该已经阅读过了。任何想法为什么没有找到?我在uploads文件夹及其中搜索了它,但尚未评估,这意味着,从文件名的读取中找不到它。有什么想法我应该如何校准它以使其工作?我搜索了一些解决方案,但它们是用于声明文件名的文件,并且它们只是添加了“r”,但是在我的上面我正在使用变量 - &gt; filename
但是当我通过"r" + filename
添加“r”时,它仍然无法正常工作,很抱歉这是一个傻瓜,但我正在努力解决这个问题。需要帮助
答案 0 :(得分:1)
您正在使用相对路径,工作目录与您的期望不同。使用绝对路径或确保相对路径以正确的工作目录为根。