Larz โบ File uploads in Python
File uploads in Python, done right
Multipart form parsing without the deprecated cgi module and without a
framework that fights you. In Larz, uploads land on req.files.
pip install larz
from larz.storage import LocalStorage
store = LocalStorage("uploads", url_prefix="/files").mount(app)
@app.post("/upload")
def upload(req):
f = req.files.get("avatar")
if not f: return ("no file", 400)
name = store.save(f) # safe, unique filename
return {"url": store.url(name), "size": f.size}
What Larz handles
- Multipart parsing โ
req.filesgives youUploadedFileobjects (.filename,.content_type,.data,.size,.save(path)); text fields still appear onreq.form. - Safe storage โ
LocalStoragestrips path traversal, makes unique names, and can serve the files back at a URL prefix. - A pluggable backend โ swap
LocalStoragefor your own (S3-style) saver implementing save/open/url/delete.
FAQ
Does Larz use the cgi module?
No โ the cgi module is deprecated and removed in Python 3.13. Larz parses multipart/form-data with its own standard-library parser.
Where do uploaded files go?
Wherever your storage backend puts them. LocalStorage writes to a directory with safe, unique names and can serve them at a URL prefix.