470,581 Members | 2,433 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 470,581 developers. It's quick & easy.

Very stupid question.

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.

Mar 30 '06 #1
6 1240
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.

Mar 30 '06 #2
"Sullivan WxPyQtKinter" wrote:
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>

Mar 30 '06 #3
"Sullivan WxPyQtKinter" <su***********@gmail.com> writes:
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 <go***@ieee.org>

"Quidquid latine dictum sit, altum sonatur."
- Qualquer coisa dita em latim soa profundo.
- Anything said in Latin sounds smart.
Mar 30 '06 #4

On 3/30/06, *Sullivan Zheng* <su***********@gmail.com> wrote:
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.
--
Benji York
Mar 30 '06 #5
On 3/30/06, *Sullivan Zheng* <su***********@gmail.com
<mailto:su***********@gmail.com>> wrote:

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:
from path import path
path('foobar').getsize()

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

Mar 30 '06 #6
Peter Hansen <pe***@engcorp.com> wrote:
>>> from path import path
>>> path('foobar').getsize()

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.

--
Juha-Matti Tapio - fil.yo. - +358-50-5419230
Mar 31 '06 #7

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

2 posts views Thread by Strange Cat | last post: by
4 posts views Thread by Piedro | last post: by
14 posts views Thread by Peter | last post: by
2 posts views Thread by Ron Weldy | last post: by
6 posts views Thread by Adam Smith | last post: by
50 posts views Thread by diffuser78 | last post: by
2 posts views Thread by Lynx101 | last post: by
1 post views Thread by livre | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.