title: Uploading file in python flask
body: <p>I am trying to incorporate uploading a basic text/csv file on my web app which runs flask to handle http requests. I tried to follow the baby example in flasks documentation running on localhost <a href="http://flask.pocoo.org/docs/0.10/patterns/fileuploads/" rel="noreferrer">here</a>. But when I try this code on my page it seems to upload but then just hangs and in fact my flask server freezes and I have to close terminal to try again...Ctrl+C doesn't even work.</p>
  
  <p>I execute <code>run.py</code>:</p>
  
  <pre><code>#!/usr/bin/env python
  from app import app
  
  if __name__ == '__main__':
      app.run(host='0.0.0.0', port=5000, debug=False, use_reloader=False)
  </code></pre>
  
  <p>and <code>app</code> is a directory in the same directory where <code>run.py</code> is with the following <code>__init__.py</code>:</p>
  
  <pre><code>import os
  from flask import Flask
  from werkzeug import secure_filename
  
  #Flask object initialization
  #app flask object has to be created before importing views below
  #because it calls "import app from app"
  UPLOAD_FOLDER = '/csv/upload'
  ALLOWED_EXTENSIONS = set(['txt', 'csv'])
  
  app = Flask(__name__)
  app.config['UPLOAD_FOLDER'] = UPLOAD_FOLDER
  </code></pre>
  
  <p>and here is my <code>views.py</code> file which has all my routes:</p>
  
  <pre><code>from flask import render_template, request, redirect, url_for
  from app import app
  import os
  
  #File extension checking
  def allowed_filename(filename):
      return '.' in filename and filename.rsplit('.',1)[1] in ALLOWED_EXTENSIONS
  
  @app.route('/', methods=['GET', 'POST'])
  @app.route('/index.html', methods=['GET', 'POST'])
  def index():
      if request.method == 'POST':
          submitted_file = request.files['file']
          if submitted_file and allowed_filename(submitted_file):
              filename = secure_filename(submitted_file.filename)
              submitted_file.save(os.path.join(app.config['UPLOAD_FOLDER'], filename))
              return redirect(url_for('uploaded_file', filename=filename))
  
      return '''
      &lt;!doctype html&gt;
      &lt;title&gt;Upload new File&lt;/title&gt;
      &lt;h1&gt;Upload new File&lt;/h1&gt;
      &lt;form action="" method=post enctype=multipart/form-data&gt;
        &lt;p&gt;&lt;input type=file name=file&gt;
           &lt;input type=submit value=Upload&gt;
      &lt;/form&gt;
      '''
  </code></pre>