File object question

Ben Hutchings ben-public-nospam at decadentplace.org.uk
Fri Dec 23 22:34:15 EST 2005


S. D. Rose <s_david_rose at hotmail.com> wrote:
> "Kent Johnson" <kent at kentsjohnson.com> wrote in message
> news:43ab3f2c_1 at newspeer2.tds.net...
>> S. D. Rose wrote:
>> > Hello all.
>> >   If I read a binary file:
>> >
>> > file = open('c:\\logo.gif', 'rw'') # Read from FS as one way to get the
>> > object, d/l from website another...
>> > file.read()
>> >
>> > is there anyway I can determine the 'size' of the object file? (Without
>> > going to the filesystem and reading the filesize from the directory ...)
>>
>> Once you have read the data you can get the size of that:
>> d = file.read()
>> print len(d)
>>
>> Is that what you mean? Otherwise I don't know how you can get the size of
>> a file without
>> asking the filesystem...
>
> Yes, len() will do what I want.  I didn't realize it would work with binary,

The read() function returns a string (object of type str), whether the
file was opened in binary or text mode.  Such objects are really a
series of bytes which may or may not represent text in some encoding. 
(By contrast, objects of type unicode definitely do represent text in
some Unicode encoding.)

> I thought it was good only for variables, lists, etc.

Variables?  Do you mean tuples?  len() works with any kind of sequence,
and strings behave as a sequence of 1-byte strings.

-- 
Ben Hutchings
It is easier to write an incorrect program than to understand a correct one.



More information about the Python-list mailing list