2017-04-18 7 views
-1

この私のコード:IOError:ファイルfile.csvが存在しません。フラスコの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アップロードを取得し、アプリのパンダを経由して、それを操作し、それをユーザーに返します。しかし、どういうわけか、私はこのトレースバックを取得したときにここにこだわってしまいました。コンソール上のエラーはなく、ウェブサイトのエラーログだけでした。それはトレースバックでファイルの正しい名前を述べたとき

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このような邪魔になることは申し訳ありませんが、真剣にこれで苦労しています。助けが必要

+0

これを[mcve]に減らしてください。 – davidism

答えて

1

あなたは相対パスを使用していますが、作業ディレクトリは期待どおりです。絶対パスを使用するか、相対パスが正しい作業ディレクトリにあることを確認してください。

+0

これは助けました:)感謝しました。 :)しかし、今問題は、それが正常に予測するとき、それは404になる。笑。ユーザーにファイルを返す代わりに、 – dekt

+0

あなたのソリューションはうまくいきましたが、予測した後、ファイルに戻りませんでした。ここでは返されませんでした。return send_from_directory(app.config ['UPLOAD_FOLDER']、ファイル名) ' – dekt

+0

別の問題ですが、あなたはそれについて何か知っているかもしれません:) – dekt

関連する問題