Practice - 5 Tasks
Answer the questions below
1fill in blank
easyComplete the code to check the file extension before upload.
Cybersecurity
if filename.endswith([1]): print("File accepted")
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Choosing executable extensions like .exe or .bat which are unsafe.
✗ Incorrect
Only files with the .jpg extension are accepted here to reduce risk.
2fill in blank
mediumComplete the code to limit the maximum file size to 5MB.
Cybersecurity
if file_size [1] 5 * 1024 * 1024: print("File size is acceptable")
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using > or >= which would accept files larger than 5MB.
✗ Incorrect
The file size should be less than or equal to 5MB to be accepted.
3fill in blank
hardFix the error in the code that checks for allowed MIME types.
Cybersecurity
allowed_types = ['image/png', 'image/jpeg'] if file_mime_type [1] allowed_types: print("MIME type allowed")
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using == which compares to the whole list, not membership.
✗ Incorrect
The in operator checks if the MIME type is in the allowed list.
4fill in blank
hardFill both blanks to safely save the uploaded file with a unique name.
Cybersecurity
import uuid unique_name = str(uuid.[1]()) + [2] + file_extension save_file(unique_name)
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using uuid1 which is time-based and less random.
Joining with underscore instead of dot.
✗ Incorrect
Using uuid4() generates a random unique ID, and "." joins the name and extension safely.
5fill in blank
hardFill all three blanks to create a dictionary comprehension filtering safe files by size and extension.
Cybersecurity
safe_files = {file: size for file, size in files.items() if file.endswith([1]) and size [2] [3] Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using unsafe extensions like .exe.
Using > instead of <= for size.
✗ Incorrect
This comprehension keeps files ending with .png and size less than or equal to 5MB.