In this tutorial we are going to learn, how to upload files using Python and flask framework. We will also upload multiple files. In a previous tutorial we explored How to join wav audio files using python – a beginner tutorial. Following steps are performed in this tutorial.
1. Python Installation
2. Create a project folder
3. Install flask framework
4. Create a templates folder
5. Create uploads folder to save files
6. Add CSS styles
7. Create a HTML form to upload files using python
7. Create a Python script to upload a single file
5. Update python script to upload multiples file
Install Python
To install Python, Visit Download Python page and install on your system.
Create a project folder
Create project folder in a specified location, and name its upload_files_using_python and cd into the the folder by using command
cd upload_files_using_python
Install flask
Type command below to install flask.
pip install flask
Create a templates folder
Inside project folder create a folder named templates.
Create a uploads folder to save files
Create a folder uploads in project directory.
Add CSS styles
Add a folder css and a file style.css. Add code below into it
In the code above, a form is created with an input named file. There is an if condition to show msg from server. To upload a file to server, the enctype = “multipart/form-data” is added in the form.
Upload a file using python script
After creating HTML form, add python code to upload file to server. Add a file upload.py in root project directory.
Directory named uploads is already created, where the uploaded files are saved.
First, flask, render_template, request and os modules are imported.
The flask constructor, takes name of current module.
app = Flask(__name__)
Next, the config is added for UPLOAD_DIR to save uploaded files. Upload route is added for GET and POST HTTP methods using @app.route method. The route will be /upload to access the server.
def upload():
The code below check that the data is posted using POST method. The uploaded file is assigned to file variable. The file is saved to the specified path. After file upload the html file is rendered using render_template and a success message is assigned.
When page is loaded first time the HTML file is rendered.
return render_template("upload.html", msg = "")
if __name__ == "__main__":
app.run()
The app.run executes the code in the file.
Run the application for upload files using python
Open command line, execute the command.
python upload.py
A message will be displayed.
Running on http://127.0.0.1:5000/ (Press CTRL+C to quit)
Open browser and add the URL in address bar.
http://127.0.0.1:5000
After file upload the success message is displayed.
Update python script to upload multiples file
To upload multiple file, add the parameter multiple to the input type = file.
<input type="file" class="form-control" name="file" placeholder="select a file to upload" required multiple>
Open upload.php file, and replace following code.
from flask import Flask, render_template, request
import os
app = Flask(__name__)
app.config["UPLOAD_DIR"] = "uploads"
@app.route("/upload", methods = ["GET", "POST"])
def upload():
if request.method == 'POST':
for file in request.files.getlist('file'):
file.save(os.path.join(app.config['UPLOAD_DIR'], file.filename))
return render_template("upload.html", msg = "File uplaoded successfully.")
return render_template("upload.html", msg = "")
if __name__ == "__main__":
app.run()
To upload multiple files a loop is added and getlist() method of request.files is added, each file is saved to the specified directory.
Source Code for the project
The source code of the tutorial is uploaded to the code repository. You can download or clone the repository.
Summary:
In this tutorial you learned about how to upload files using python and flask. HTML form and a Python script is added and updated to upload multiple files. The source code of tutorial can be found on GitHub repository.