title: Uploading and reading a CSV file with Flask
body: <p>I'm currently in the process of making a program to upload and read csv files. I'm throwing a key error when submitting a file to be uploaded and can't really seem to figure out why and was hoping for some help. It uploaded and saved the file before I tried adding the read file functionality but after that, it started having issues. The error is saying that 'filename' is a key error even though it seemed to work fine before I tried reading the file. Help or leading me down the right path would be greatly appreciated. Thanks so much!</p>
  <p>Views.py</p>
  <pre><code>from flask import render_template, request, redirect
  from app import app
  import os
  import csv
  
  
  @app.route('/', methods=[&quot;GET&quot;, &quot;POST&quot;])
  def index():
      data = []
      if request.method == 'POST':
          if request.files:
              uploaded_file = request.files['filename'] # This line uses the same variable and worked fine
              uploaded_file.save(os.path.join(app.config['FILE_UPLOADS'], uploaded_file.filename))
              f = request.form['filename'] # This is the line throwing the error
              with open(f) as file:
                  csv_file = csv.reader(file)
                  for row in csv_file:
                      data.append(row)
              return redirect(request.url)
      return render_template('index.html', data=data)
  
  
  @app.route('/help')
  def help():
      return render_template('help.html')
  
  app.config['FILE_UPLOADS'] = &quot;C:\\Users\\Zachary\\Documents\\VSCode_Projects\\monday_webapp\\app\\static\\file\\uploads&quot;
  </code></pre>
  <p>Index.html</p>
  <pre><code>{% extends &quot;base.html&quot; %}
  {% block title %}Home{% endblock %}
  {% block body %}
  
  &lt;div class=&quot;jumbotron&quot;&gt;
      &lt;h1 style='text-align: center'&gt;Zach's Web Application&lt;/h1&gt;
  &lt;/div&gt;
  &lt;div&gt;
      &lt;p class=&quot;lead&quot;&gt;Upload a csv file to view its data.&lt;/p&gt;
      &lt;form method=&quot;POST&quot; enctype=&quot;multipart/form-data&quot; action=&quot;/&quot;&gt;
          &lt;input type=&quot;file&quot; id=&quot;myFile&quot; name=&quot;filename&quot; accept=&quot;.csv&quot;&gt;
          &lt;input type=&quot;submit&quot;&gt;
      &lt;/form&gt;
  &lt;/div&gt;
  &lt;div&gt;
      {{ data }}
  &lt;/div&gt;
  &lt;div&gt;
  
  {% endblock %}
  </code></pre>