How do you measure the size of a cgi.FieldStorage instance?

When a file is loaded, the standard way of representing it is cgi.FieldStorage

(at least that's how Pyramid does it). However, if I want to know the length efficiently, how do I get it? For example:

uploaded = cgi.FieldStorage()

      

If this variable comes from a query, how do you determine the file size?

+3


source to share


1 answer


The least efficient way would be the following:

size = len(uploaded.value)

      

it's 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 latter option is the preferred way of measuring a file in C and Python by proxy only. It returns a structure that can tell you all sorts of things, even the file size.

+5


source







All Articles