File object question

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • S. D. Rose

    #1

    File object question

    Hello all.
    If I read a binary file:

    file = open('c:\\logo. gif', 'rw'') # Read from FS as one way to get the
    object, d/l from website another...
    file.read()

    is there anyway I can determine the 'size' of the object file? (Without
    going to the filesystem and reading the filesize from the directory ...)

    Thanks!



  • mrmakent@cox.net

    #2
    Re: File object question

    See http://www.python.org/doc/2.4.2/lib/os-file-dir.html for the 'stat'
    function

    import os
    os.stat(path).s t_size

    This will return the size in bytes of the file designated by 'path'.

    Comment

    • Kent Johnson

      #3
      Re: File object question

      S. D. Rose wrote:[color=blue]
      > Hello all.
      > If I read a binary file:
      >
      > file = open('c:\\logo. gif', 'rw'') # Read from FS as one way to get the
      > object, d/l from website another...
      > file.read()
      >
      > is there anyway I can determine the 'size' of the object file? (Without
      > going to the filesystem and reading the filesize from the directory ...)[/color]

      Once you have read the data you can get the size of that:
      d = file.read()
      print len(d)

      Is that what you mean? Otherwise I don't know how you can get the size of a file without
      asking the filesystem...

      Kent

      Comment

      • S. D. Rose

        #4
        Re: File object question

        Yes, len() will do what I want. I didn't realize it would work with binary,
        I thought it was good only for variables, lists, etc.

        Thanks!
        -Dave

        "Kent Johnson" <kent@kentsjohn son.com> wrote in message
        news:43ab3f2c_1 @newspeer2.tds. net...[color=blue]
        > S. D. Rose wrote:[color=green]
        > > Hello all.
        > > If I read a binary file:
        > >
        > > file = open('c:\\logo. gif', 'rw'') # Read from FS as one way to get the
        > > object, d/l from website another...
        > > file.read()
        > >
        > > is there anyway I can determine the 'size' of the object file? (Without
        > > going to the filesystem and reading the filesize from the directory ...)[/color]
        >
        > Once you have read the data you can get the size of that:
        > d = file.read()
        > print len(d)
        >
        > Is that what you mean? Otherwise I don't know how you can get the size of[/color]
        a file without[color=blue]
        > asking the filesystem...
        >
        > Kent
        > --
        > http://mail.python.org/mailman/listinfo/python-list
        >[/color]



        Comment

        • Ben Hutchings

          #5
          Re: File object question

          S. D. Rose <s_david_rose@h otmail.com> wrote:[color=blue]
          > "Kent Johnson" <kent@kentsjohn son.com> wrote in message
          > news:43ab3f2c_1 @newspeer2.tds. net...[color=green]
          >> S. D. Rose wrote:[color=darkred]
          >> > Hello all.
          >> > If I read a binary file:
          >> >
          >> > file = open('c:\\logo. gif', 'rw'') # Read from FS as one way to get the
          >> > object, d/l from website another...
          >> > file.read()
          >> >
          >> > is there anyway I can determine the 'size' of the object file? (Without
          >> > going to the filesystem and reading the filesize from the directory ...)[/color]
          >>
          >> Once you have read the data you can get the size of that:
          >> d = file.read()
          >> print len(d)
          >>
          >> Is that what you mean? Otherwise I don't know how you can get the size of
          >> a file without
          >> asking the filesystem...[/color]
          >
          > Yes, len() will do what I want. I didn't realize it would work with binary,[/color]

          The read() function returns a string (object of type str), whether the
          file was opened in binary or text mode. Such objects are really a
          series of bytes which may or may not represent text in some encoding.
          (By contrast, objects of type unicode definitely do represent text in
          some Unicode encoding.)
          [color=blue]
          > I thought it was good only for variables, lists, etc.[/color]

          Variables? Do you mean tuples? len() works with any kind of sequence,
          and strings behave as a sequence of 1-byte strings.

          --
          Ben Hutchings
          It is easier to write an incorrect program than to understand a correct one.

          Comment

          Working...