Very stupid question.

  • Thread starter Sullivan WxPyQtKinter
  • Start date
S

Sullivan WxPyQtKinter

How to get the length of a file via build-in file object support? In
Visual Basic there is len(file) of something like that. But in python,
where is this property?

Sorry for this stupid question, if it is.

Thank you for help.
 
S

Sullivan WxPyQtKinter

In addition, f=file('filename','r');len(f.read()) is quite expensive in
my point of view, If the file is serveral MB or larger.
 
F

Fredrik Lundh

Sullivan WxPyQtKinter said:
How to get the length of a file via build-in file object support? In
Visual Basic there is len(file) of something like that. But in python,
where is this property?

import os

size = os.path.getsize(filename)

</F>
 
J

Jorge Godoy

Sullivan WxPyQtKinter said:
How to get the length of a file via build-in file object support? In
Visual Basic there is len(file) of something like that. But in python,
where is this property?

Sorry for this stupid question, if it is.

pydoc os and then look for "stat"... In "stat_result" there's a
description of the tuple you'll get.

--
Jorge Godoy <[email protected]>

"Quidquid latine dictum sit, altum sonatur."
- Qualquer coisa dita em latim soa profundo.
- Anything said in Latin sounds smart.
 
B

Benji York

> Wow, seems I am not that supid. Why python does not include this
> function in the file object. It is almost a tradition in other
> languages...
> really not elegant or OO.

A file isn't an object.

You can get a "file object" by opening a file (on disk), but it
doesn't make much sense to have to open a file just to see how big it
is.
 
P

Peter Hansen

Wow, seems I am not that supid. Why python does not include this
function in the file object. It is almost a tradition in other
languages...

import os

os.stat(path).st_size

really not elegant or OO.

You might find something like Jason Orendorff's path.py module (Google
for it) to be more elegant. With it, this works fine:
12345L

(But note that it's just a nice wrapper around the scattered builtin
ways of doing the same thing, in this case the os.stat().st_size
approach mentioned above. That's not a bad thing, though, IMHO.)

-Peter
 
J

Juha-Matti Tapio

Peter Hansen said:
12345L
(But note that it's just a nice wrapper around the scattered builtin
ways of doing the same thing, in this case the os.stat().st_size
approach mentioned above. That's not a bad thing, though, IMHO.)

Also if the file in question is already open, it can be done like this:

os.fstat(fileobject.fileno()).st_size

This form avoids some race condition scenarious with the file being changed
between stat and open.

I think file sizes should be used carefully and only for visual reasons.
For code logic it is almost always better to go the "it's easier to ask
forgiveness than ask permission" -route. Therefore looking up the file size
is done only rarely and it is not worthy to be a file-object method.
 

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

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,053
Latest member
BrodieSola

Latest Threads

Top