📖 Scenario: You are building a simple Flask web app that allows users to upload text files. The app will save these files on the server and then display the contents back to the user. This shows why handling files is important in web apps for storing and retrieving user data.
🎯 Goal: Create a Flask app that accepts a text file upload, saves it on the server, and then reads and displays the file content on a new page.
📋 What You'll Learn
Create a Flask app with a route to upload files
Set up a folder path variable to save uploaded files
Write code to save the uploaded file to the folder
Read the saved file and display its content on a new page
💡 Why This Matters
🌍 Real World
Web apps often let users upload files like images, documents, or data. Handling file operations lets the app save and retrieve these files safely.
💼 Career
Understanding file operations in Flask is key for backend web development roles that involve user data management and server-side storage.
Progress0 / 4 steps
1
Set up Flask app and upload route
Import Flask, request, and render_template from flask. Create a Flask app instance called app. Define a route /upload that accepts GET and POST methods.
Flask
Hint
Start by importing Flask and creating the app instance. Then define the upload route with GET and POST methods.
2
Add upload folder configuration
Create a variable called UPLOAD_FOLDER and set it to the string 'uploads/'. Add this variable to the Flask app config as app.config['UPLOAD_FOLDER'] = UPLOAD_FOLDER.
Flask
Hint
Define the folder path where uploaded files will be saved and add it to the app config.
3
Save uploaded file to the server
Inside the upload function, check if the request method is POST. If yes, get the file from request.files['file']. Use file.save() to save the file to os.path.join(app.config['UPLOAD_FOLDER'], file.filename). Return a string 'File saved!' after saving.
Flask
Hint
Check for POST method, get the file from the form, save it to the upload folder, and confirm saving.
4
Read and display saved file content
Create a new route /display/<filename>. In its function, open the file from os.path.join(app.config['UPLOAD_FOLDER'], filename) in read mode. Read the content and return it inside a simple HTML paragraph tag.
Flask
Hint
Create a route that takes a filename, reads the file content, and returns it wrapped in a paragraph tag.
Practice
(1/5)
1. Why are file operations important in Flask web applications?
easy
A. They allow the app to save and read user data like uploads or logs.
B. They improve the speed of the Flask server automatically.
C. They help Flask apps run without internet connection.
D. They replace the need for a database entirely.
Solution
Step 1: Understand file operations role
File operations let the app save or read files, such as user uploads or logs.
Step 2: Compare other options
Speed improvements, offline running, or replacing databases are not direct results of file operations.
Final Answer:
They allow the app to save and read user data like uploads or logs. -> Option A
Quick Check:
File operations = save/read user data [OK]
Hint: File ops = handling user files and data [OK]
Common Mistakes:
Thinking file ops speed up Flask automatically
Believing file ops replace databases
Assuming file ops make apps offline
2. Which is the correct way to open a file for writing in a Flask app?
easy
A. file.open('file.txt', 'w')
B. open('file.txt', 'r')
C. with open('file.txt', 'w') as f:
D. open('file.txt', 'x')
Solution
Step 1: Identify correct syntax for writing files
Using 'with open(filename, 'w') as f:' safely opens a file for writing and auto-closes it.
Step 2: Check other options
'r' is for reading, 'file.open' is invalid syntax, 'x' is for exclusive creation but not common for writing.
Final Answer:
with open('file.txt', 'w') as f: -> Option C
Quick Check:
Use 'with open' + 'w' to write files [OK]
Hint: Use 'with open' and 'w' mode to write files [OK]
Common Mistakes:
Using 'r' mode to write files
Trying 'file.open' instead of 'open'
Confusing 'x' mode with 'w' mode
3. What will this Flask code print if 'data.txt' contains 'Hello Flask'?
with open('data.txt', 'r') as file:
content = file.read()
print(content)
medium
A. Hello Flask
B. file object
C. Error: file not found
D. None
Solution
Step 1: Understand file reading code
The code opens 'data.txt' in read mode and reads all content into 'content'.
Step 2: Check file content and output
The file contains 'Hello Flask', so print(content) outputs that exact string.
Final Answer:
Hello Flask -> Option A
Quick Check:
Reading file prints its content [OK]
Hint: Reading file prints its text content [OK]
Common Mistakes:
Expecting print to show file object
Assuming file missing error without checking
Thinking print returns None
4. Identify the error in this Flask file write code: