Bird
Raised Fist0
Flaskframework~20 mins

Saving uploaded files in Flask - Practice Problems & Coding Challenges

Choose your learning style10 modes available
Challenge - 5 Problems
🎖️
File Upload Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ component_behavior
intermediate
2:00remaining
What happens when this Flask code saves an uploaded file?

Consider this Flask route that handles a file upload and saves it:

from flask import Flask, request
app = Flask(__name__)

@app.route('/upload', methods=['POST'])
def upload():
    file = request.files['file']
    file.save('/tmp/' + file.filename)
    return 'File saved'

What will happen if a user uploads a file named example.txt?

Flask
from flask import Flask, request
app = Flask(__name__)

@app.route('/upload', methods=['POST'])
def upload():
    file = request.files['file']
    file.save('/tmp/' + file.filename)
    return 'File saved'
AThe file will be saved in the /tmp directory with the name example.txt
BThe file will be saved in the current directory with the name example.txt
CThe code will raise a KeyError because 'file' is not in request.files
DThe file will be saved but with a random generated filename
Attempts:
2 left
💡 Hint

Look at the file.save line and the path used.

📝 Syntax
intermediate
2:00remaining
Which option correctly saves an uploaded file in Flask?

Which of the following Flask code snippets correctly saves an uploaded file to the 'uploads' folder?

A
file = request.files['file']
file.save('uploads/' + file.filename)
B
file = request.files.get('file')
file.save('uploads')
C
file = request.files['file']
file.save('uploads')
D
file = request.files['file']
file.save('uploads/' + file)
Attempts:
2 left
💡 Hint

Remember that file.save() needs a full path including filename.

🔧 Debug
advanced
2:00remaining
Why does this Flask file upload code raise an error?

Given this Flask route:

@app.route('/upload', methods=['POST'])
def upload():
    file = request.files['file']
    file.save('uploads/' + file.filename)
    return 'Saved'

When a user uploads a file, the server raises a FileNotFoundError. Why?

Flask
@app.route('/upload', methods=['POST'])
def upload():
    file = request.files['file']
    file.save('uploads/' + file.filename)
    return 'Saved'
AThe file object does not have a filename attribute
BThe 'uploads' directory does not exist on the server
CThe request method is not POST
DThe file is too large to save
Attempts:
2 left
💡 Hint

Check if the folder path exists before saving files.

❓ state_output
advanced
2:00remaining
What is the value of file.filename after upload?

In Flask, after a user uploads a file named photo.png, what is the value of file.filename in this code?

file = request.files['file']
Flask
file = request.files['file']
AAn empty string ''
B'file'
CThe full file path on the user's computer
D'photo.png'
Attempts:
2 left
💡 Hint

Think about what file.filename represents in Flask uploads.

🧠 Conceptual
expert
3:00remaining
Which option best prevents filename conflicts when saving uploaded files in Flask?

When saving uploaded files in Flask, how can you avoid overwriting existing files with the same name?

AAlways save files with their original filename in the same folder
BOverwrite existing files with the new upload to keep latest version
CGenerate a unique filename using a UUID and save the file with it
DSave files only in memory without writing to disk
Attempts:
2 left
💡 Hint

Think about how to keep filenames unique to avoid conflicts.

Practice

(1/5)
1. In Flask, which object do you use to access files uploaded by a user in a form?
easy
A. request.args
B. request.files
C. request.data
D. request.form

Solution

  1. Step 1: Understand Flask request objects

    Flask provides different objects to access data sent by the client. request.files holds uploaded files.
  2. Step 2: Identify correct object for files

    request.form is for form fields, request.data is raw data, and request.args is for URL query parameters.
  3. Final Answer:

    request.files -> Option B
  4. Quick Check:

    Uploaded files = request.files [OK]
Hint: Uploaded files are always in request.files [OK]
Common Mistakes:
  • Using request.form to get files
  • Trying to access files from request.args
  • Confusing request.data with files
2. Which of the following is the correct way to save an uploaded file named file to a folder called uploads in Flask?
easy
A. file.save('uploads/' + file.filename)
B. file.save(file.filename)
C. save(file, 'uploads/')
D. request.save(file, 'uploads/')

Solution

  1. Step 1: Use the file object's save method

    The uploaded file object has a save() method that takes the full path to save the file.
  2. Step 2: Provide correct path including folder and filename

    Concatenate the folder path and the file's filename to save it in the right place.
  3. Final Answer:

    file.save('uploads/' + file.filename) -> Option A
  4. Quick Check:

    Use file.save(path) with full path [OK]
Hint: Use file.save('folder/' + file.filename) to save files [OK]
Common Mistakes:
  • Calling save() without a path
  • Using save() as a standalone function
  • Not including folder path in save()
3. What will happen when this Flask code runs after a file upload?
file = request.files['photo']
file.save('/tmp/' + file.filename)
return 'Saved!'
medium
A. Nothing is saved because request.files['photo'] is empty by default
B. The file is saved but the filename is lost, causing an error
C. The code raises an error because save() needs a full absolute path
D. The uploaded file is saved in /tmp/ with its original filename and 'Saved!' is returned

Solution

  1. Step 1: Access the uploaded file from request.files

    The code gets the file named 'photo' from the uploaded files correctly.
  2. Step 2: Save the file to /tmp/ folder with original filename

    Calling save() with '/tmp/' + filename saves the file in that folder.
  3. Final Answer:

    The uploaded file is saved in /tmp/ with its original filename and 'Saved!' is returned -> Option D
  4. Quick Check:

    save() stores file at given path, returns confirmation [OK]
Hint: save() stores file at given path, returns confirmation [OK]
Common Mistakes:
  • Assuming save() needs absolute path always
  • Thinking request.files['photo'] is empty without upload
  • Confusing save() behavior with form data saving
4. Identify the error in this Flask code snippet for saving an uploaded file:
file = request.files['file']
file.save(file.filename)
medium
A. Missing folder path in save() can cause files to save in wrong location
B. Using request.files instead of request.form causes error
C. file.save() requires a file object, not a filename string
D. file.filename is not a valid attribute of the file object

Solution

  1. Step 1: Check the save() method argument

    The code saves the file using only the filename, without specifying a folder path.
  2. Step 2: Understand file saving location

    Without a folder path, the file saves in the current working directory, which may be unintended or insecure.
  3. Final Answer:

    Missing folder path in save() can cause files to save in wrong location -> Option A
  4. Quick Check:

    Always include folder path in save() [OK]
Hint: Always include folder path in save() to control file location [OK]
Common Mistakes:
  • Not specifying folder path in save()
  • Confusing request.files with request.form
  • Assuming file.filename is invalid
5. You want to securely save an uploaded file in Flask to the uploads folder, avoiding unsafe filenames. Which code snippet correctly achieves this?
from werkzeug.utils import secure_filename
file = request.files['file']
filename = secure_filename(file.filename)
file.save(f'uploads/{filename}')
hard
A. The code will fail because secure_filename() returns a file object, not a string
B. secure_filename() is unnecessary; file.save() cleans filenames automatically
C. This code safely saves the file with a cleaned filename in uploads folder
D. You must manually create the uploads folder before saving, or it errors

Solution

  1. Step 1: Use secure_filename to clean the filename

    The function secure_filename() removes unsafe characters to prevent security issues.
  2. Step 2: Save the file with the cleaned filename in the uploads folder

    Using file.save() with the cleaned filename inside the uploads folder saves the file securely.
  3. Final Answer:

    This code safely saves the file with a cleaned filename in uploads folder -> Option C
  4. Quick Check:

    Use secure_filename() before saving uploaded files [OK]
Hint: Use secure_filename() to avoid unsafe file names [OK]
Common Mistakes:
  • Skipping secure_filename and risking unsafe filenames
  • Assuming save() cleans filenames automatically
  • Not creating uploads folder before saving
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard