getting file size

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Bob Smith

    #1

    getting file size

    Are these the same:

    1. f_size = os.path.getsize (file_name)

    2. fp1 = file(file_name, 'r')
    data = fp1.readlines()
    last_byte = fp1.tell()

    I always get the same value when doing 1. or 2. Is there a reason I
    should do both? When reading to the end of a file, won't tell() be just
    as accurate as os.path.getsize ()?

    Thanks guys,

    Bob
  • John Machin

    #2
    Re: getting file size


    Bob Smith wrote:[color=blue]
    > Are these the same:
    >
    > 1. f_size = os.path.getsize (file_name)
    >
    > 2. fp1 = file(file_name, 'r')
    > data = fp1.readlines()
    > last_byte = fp1.tell()
    >
    > I always get the same value when doing 1. or 2. Is there a reason I
    > should do both? When reading to the end of a file, won't tell() be[/color]
    just[color=blue]
    > as accurate as os.path.getsize ()?
    >[/color]

    Read the docs. Note the hint that you get what the stdio serves up.
    ftell() can only be _guaranteed_ to give you a magic cookie that you
    may later use with fseek(magic_coo kie) to return to the same place in a
    more reliable manner than with Hansel & Gretel's non-magic
    bread-crumbs. On 99.99% of modern filesystems, the cookie obtained by
    ftell() when positioned at EOF is in fact the size in bytes. But why
    chance it? os.path.getsize does as its name suggests; why not use it,
    instead of a method with a side-effect? As for doing _both_, why would
    you??

    Comment

    Working...