1

I've looked at a couple of the other questions but can't figure out what is going wrong. I get the following error: "FileNotFoundError: [Errno 2] No such file or directory: '/uploads\MRRtest.csv'" Can anyone help? What is the difference between the forward and backward slashes on the error message?

Thanks

from flask import Flask, render_template, request, redirect, url_for, flash
from flask.ext.bootstrap import Bootstrap
from werkzeug import secure_filename
import os


app = Flask(__name__)
bootstrap = Bootstrap(app)
UPLOAD_FOLDER = '/uploads'
ALLOWED_EXTENSIONS = set(['csv'])
app.config['UPLOAD_FOLDER'] = UPLOAD_FOLDER



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

@app.route('/', methods=['GET', 'POST'])
def index():
    if request.method == 'POST':
        file = request.files['file']
        if file and allowed_file(file.filename):
            filename = secure_filename(file.filename)
            file.save(os.path.join(app.config['UPLOAD_FOLDER'],filename))
            return redirect(url_for('uploaded_file', filename=filename))
    return render_template('index.html')

My index.html template is as follows:

{% extends "base.html" %}

{% block title %}Flasky{% endblock %}

{% block page_content %}
<div class="page-header">
    <h1>Upload File</h1>
    <form action="" method=post enctype=multipart/form-data>
      <p><input type=file name=file>
         <input type=submit value=Upload>
    </form>
</div>
{% endblock %}
davidism
  • 121,510
  • 29
  • 395
  • 339

2 Answers2

5

/uploads means an absolute link (C:/upload), so you should use upload/ instead.

Also, you can use the nice snippet from https://stackoverflow.com/a/20257725/5851179

APP_ROOT = os.path.dirname(os.path.abspath(__file__))
UPLOAD_FOLDER = os.path.join(APP_ROOT, 'static/uploads')
app.config['UPLOAD_FOLDER'] = UPLOAD_FOLDER
Community
  • 1
  • 1
Cal Eliacheff
  • 236
  • 1
  • 12
0

Just realized from the comments that my uploads directory is in the same directory as my run.py file rather than the 'templates' directory that index.html is running from. I modified the

UPLOAD_FOLDER = '/uploads'

to

UPLOAD_FOLDER = './uploads'

I'll now work on building the "url for endpoint"

Thanks for your help.