[Zope-dev] zope.filerepresentation

Laurence Rowe l at lrowe.co.uk
Thu Oct 1 11:25:20 EDT 2009


2009/10/1 Martin Aspeli <optilude+lists at gmail.com>:
> Hanno Schlichting wrote:
>> The standard file implementation has no knowledge of its size, as this
>> is sometimes impossible to get, when dealing with stream based
>> file-like objects. Do we really need to have files to know their size?
>
> Well, for the writeFile() stuff maybe we don't. Thinking through my use
> cases again, I can't see a need for passing the content type in, and
> encoding can be set if we support setting the '.encoding' property.
>
> It's kind of important to be able to indicate the size and MIME type for
> a read operation, though. In this case, I want to be able to put that
> information into the Content-Type and Content-Length headers. The
> IStreamData stuff in Zope 2 also wants to know the length before it
> starts streaming.
>
> In some cases, it may not be possible to know, in which case we can fall
> back on len(data), but in other cases, the length is known
> (plone.namedfile and z3c.blobfile keep track of it, for example), in
> which case having a way to ask the adapter means it can be done much
> more efficiently.

To find the length of a file:

    f.seek(0,2) # 0 bytes from the end of the file
    size = f.tell() # position in file.

Laurence


More information about the Zope-Dev mailing list