Very stupid question.

Discussion in 'Python' started by Sullivan WxPyQtKinter, Mar 30, 2006.

  1. 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.
     
    Sullivan WxPyQtKinter, Mar 30, 2006
    #1
    1. Advertising

  2. 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.
     
    Sullivan WxPyQtKinter, Mar 30, 2006
    #2
    1. Advertising

  3. "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>
     
    Fredrik Lundh, Mar 30, 2006
    #3
  4. Sullivan WxPyQtKinter

    Jorge Godoy Guest

    "Sullivan WxPyQtKinter" <> 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 <>

    "Quidquid latine dictum sit, altum sonatur."
    - Qualquer coisa dita em latim soa profundo.
    - Anything said in Latin sounds smart.
     
    Jorge Godoy, Mar 30, 2006
    #4
  5. Sullivan WxPyQtKinter

    Benji York Guest

    On 3/30/06, *Sullivan Zheng* <> 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
     
    Benji York, Mar 30, 2006
    #5
  6. Sullivan WxPyQtKinter

    Peter Hansen Guest

    On 3/30/06, *Sullivan Zheng* <
    <mailto:>> 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
     
    Peter Hansen, Mar 30, 2006
    #6
  7. Peter Hansen <> 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
     
    Juha-Matti Tapio, Mar 31, 2006
    #7
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Raymond Arthur St. Marie II of III

    very Very VERY dumb Question About The new Set( ) 's

    Raymond Arthur St. Marie II of III, Jul 23, 2003, in forum: Python
    Replies:
    4
    Views:
    483
    Raymond Hettinger
    Jul 27, 2003
  2. Brandon McCombs
    Replies:
    4
    Views:
    519
    Richard Wheeldon
    Aug 28, 2006
  3. shanx__=|;-

    very very very long integer

    shanx__=|;-, Oct 16, 2004, in forum: C Programming
    Replies:
    19
    Views:
    1,629
    Merrill & Michele
    Oct 19, 2004
  4. Peter

    Very very very basic question

    Peter, Feb 8, 2005, in forum: C Programming
    Replies:
    14
    Views:
    517
    Dave Thompson
    Feb 14, 2005
  5. rincewind

    stupid, STUPID question!

    rincewind, Apr 19, 2009, in forum: HTML
    Replies:
    25
    Views:
    1,024
Loading...

Share This Page