Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Python > Very stupid question.

Reply
Thread Tools

Very stupid question.

 
 
Sullivan WxPyQtKinter
Guest
Posts: n/a
 
      03-30-2006
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.

 
Reply With Quote
 
 
 
 
Sullivan WxPyQtKinter
Guest
Posts: n/a
 
      03-30-2006
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.

 
Reply With Quote
 
 
 
 
Fredrik Lundh
Guest
Posts: n/a
 
      03-30-2006
"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>



 
Reply With Quote
 
Jorge Godoy
Guest
Posts: n/a
 
      03-30-2006
"Sullivan WxPyQtKinter" <(E-Mail Removed)> 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 <(E-Mail Removed)>

"Quidquid latine dictum sit, altum sonatur."
- Qualquer coisa dita em latim soa profundo.
- Anything said in Latin sounds smart.
 
Reply With Quote
 
Benji York
Guest
Posts: n/a
 
      03-30-2006

On 3/30/06, *Sullivan Zheng* <(E-Mail Removed)> 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
 
Reply With Quote
 
Peter Hansen
Guest
Posts: n/a
 
      03-30-2006
On 3/30/06, *Sullivan Zheng* <(E-Mail Removed)
<(E-Mail Removed)>> 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

 
Reply With Quote
 
Juha-Matti Tapio
Guest
Posts: n/a
 
      03-31-2006
Peter Hansen <(E-Mail Removed)> 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
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Stupid question. Please, only stupid responders. If you're not sureif you're stupid, you probably aren't. =?ISO-8859-1?Q?R=F4g=EAr?= Computer Support 6 07-18-2005 05:11 AM
stupid stupid stupid kpg MCSE 17 11-26-2004 02:59 PM
very very very long integer shanx__=|;- C Programming 19 10-19-2004 03:55 PM
Quick Book file access very very very slow Thomas Reed Computer Support 7 04-09-2004 08:09 PM
very Very VERY dumb Question About The new Set( ) 's Raymond Arthur St. Marie II of III Python 4 07-27-2003 12:09 AM



Advertisments