Bird
0
0

Find the bug in this FastAPI file saving code:

medium📝 Debug Q7 of 15
FastAPI - File Handling
Find the bug in this FastAPI file saving code:
from fastapi import FastAPI, File, UploadFile
app = FastAPI()

@app.post('/upload')
async def upload(file: UploadFile = File(...)):
    with open(file.filename, 'wb') as f:
        content = await file.read()
        f.write(content)
    return {'filename': file.filename}
AFile pointer must be reset before writing
BFile should be opened before reading content
CNo bug, code works as expected
DUsing await inside with-block is correct
Step-by-Step Solution
Solution:
  1. Step 1: Review async file read and write order

    Reading file content asynchronously before writing is correct.
  2. Step 2: Check file open mode and usage

    Opening file in 'wb' mode and writing bytes is correct.
  3. Final Answer:

    No bug, code works as expected -> Option C
  4. Quick Check:

    Async read then write file = correct [OK]
Quick Trick: Read async first, then write file in binary mode [OK]
Common Mistakes:
MISTAKES
  • Thinking await can't be inside with
  • Believing file pointer reset needed
  • Confusing read/write order

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More FastAPI Quizzes