Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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?

like image 760
javex Avatar asked Feb 13 '23 06:02

javex


1 Answers

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.

like image 129
javex Avatar answered Feb 15 '23 09:02

javex