Complete the code to open a file for reading in a Flask app.
with open('data.txt', '[1]') as file: content = file.read()
The mode 'r' opens the file for reading, which is needed to read content.
Complete the code to save uploaded file content in a Flask route.
file = request.files['file'] file.save('[1]')
Files should be saved in a folder like 'uploads/' combined with the original filename.
Fix the error in the code to read a file safely in Flask.
try: with open('[1]', 'r') as f: data = f.read() except FileNotFoundError: data = ''
The file path must include the folder where files are saved, like 'uploads/data.txt'.
Fill both blanks to check if a file exists before reading it in Flask.
import os if os.path.[1]('uploads/data.txt'): with open('uploads/data.txt', '[2]') as f: content = f.read()
Use os.path.exists to check if the file exists, and 'r' mode to read it.
Fill all three blanks to create a dictionary comprehension that maps filenames to their sizes in Flask.
import os file_sizes = [1]: os.path.getsize(f'uploads/[2]') for [3] in os.listdir('uploads') if os.path.isfile(f'uploads/[2]')
The dictionary comprehension uses 'f' as the loop variable, 'f' for formatting filenames, and starts with '{f' to create the dict.