When a file is uploaded, the standard way of representing it is cgi.FieldStorage
(at least Pyramid does it that way). However, if I want to know the length in an efficient way, how do I get it? For example:
uploaded = cgi.FieldStorage()
If this variable now came from a request, how to determine the file's size?
The least efficient way would be this:
size = len(uploaded.value)
Better:
uploaded.file.seek(0, 2)
size = uploaded.file.tell()
uploaded.file.seek(0)
Best:
import os
size = os.fstat(uploaded.file.fileno()).st_size
The last option is the preferred way to measure a file's in C and Python only proxies this through. It returns a structure that can tell you all sorts of stuff, even the size of a file.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With