File object question

S

S. D. Rose

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 ...)

Thanks!
 
K

Kent Johnson

S. D. Rose said:
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...

Kent
 
S

S. D. Rose

Yes, len() will do what I want. I didn't realize it would work with binary,
I thought it was good only for variables, lists, etc.

Thanks!
-Dave
 
B

Ben Hutchings

S. D. Rose said:
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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,274
Messages
2,571,368
Members
48,060
Latest member
JerrodSimc

Latest Threads

Top