2017-04-18 57 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 

爲什麼該文件在追蹤中指出文件的正確名稱時不存在?所以它應該閱讀它。任何想法爲什麼沒有找到?我在uploads文件夾和它的那裏搜索它,但沒有被評估,這意味着,從文件名的讀取它沒有被發現。任何想法,我應該如何校準它,使這項工作?我搜索了一些解決方案,但他們是爲文件的文件名說明,他們只是添加了「r」,但在我的我使用的變量 - >filename,但當我通過做"r" + filename添加「r」它仍然does not工作,對不起,要成爲這樣的傻瓜,但嚴重即時通訊與此掙扎。需要幫助

+0

請把它降低到[mcve]。 – davidism

回答

1

您正在使用相對路徑,並且工作目錄與您期望的不同。使用絕對路徑或確保相對路徑植根於正確的工作目錄中。

+0

這有助於:)謝謝我終於明白了。 :)但現在的問題是,當它成功預測,它會達到404。而不是給用戶回他的文件 – dekt

+0

你的解決方案工作,但預測後,它不會返回到文件,這是它應該在這裏'返回send_from_directory(app.config ['UPLOAD_FOLDER'],文件名)' – dekt

+0

我知道這是另一個問題,但你可能知道一些關於它:) – dekt