File not found when reading Flask uploaded file into pandas dataframe

Viewed 1019

I am trying to create a Flask web app on PythonAnywhere using Python 2.7 that allows the user to upload a csv file and then reads the file into a pandas dataframe for processing. The program worked with one csv file I tried but not any of the others. The program seems to upload the files fine but throws an error message saying #012IOError: File xyz.csv does not exist. Here's the relevant part of the code:

from flask import Flask, make_response, request, send_file
import pandas as pd

# Initialize the Flask application
app = Flask(__name__)

@app.route('/')
def form():
    return """
        <html>
            <body>
                <h1>YDNA Kit Grouping Program</h1>

                <form action="/main_program" method="post" enctype="multipart/form-data">
                    <input type="file" name="input_file" />
                    <input type="submit" />
                </form>
            </body>
        </html>
    """

@app.route('/main_program', methods=["POST"])
def main_program_view():

    # Input file
    file = request.files['input_file']
    if not file:
        return "No file"

    # Put input file in dataframe
    df = pd.read_csv(file.filename, encoding='cp1252')

I think the problem may in the last line of code where file.filename is not giving the file location information that pd.read_csv needs. But I have no idea why it would work for one csv file but not another and no idea how to fix it so that it can read any uploaded csv file into the dataframe.

1 Answers
Related