file.read() returns an emtpy even if its currenet position is not atthe end

Discussion in 'Python' started by js, Apr 22, 2007.

  1. js

    js Guest

    Hi list.

    I'm writing a tail -f like program in python
    and I found file.read() doesn't work as I think it should.

    Here's the code illustrating my problem.

    ###
    #!/usr/bin/env python
    import os, sys
    filename = "test.out"

    f = open(filename, "w+")
    f.write("Hello")
    f.flush()

    f.seek(0, 2)

    statinfo = os.stat(filename)
    print "file size: %d" % statinfo.st_size
    print "position : %d" % f.tell()
    line = f.read()
    print "line : [%s]" % line

    # Writing the same file using another fd
    f2 = open(filename, "a+")
    f2.write("World")
    f2.flush()
    f2.close()

    statinfo = os.stat(filename)
    print "file size: %d" % statinfo.st_size
    print "position : %d" % f.tell()
    line = f.read() # read() returns emtpy!! readlines?() works ok
    ###

    Running the above, I got the following.
    ###
    file size: 5
    position : 5
    line : []
    file size: 10
    position : 5
    ###

    So my question is
    why the second f.read() returns an emtpy?
    >From tell() and its st_size I'm sure that file descriptor is not at the EOF

    and read()'s doc says
    "An empty string is returned when EOF is encountered immediately".
    Using readline() or readlines() instead of read() works great though.

    I'm using Python 2.4.3 on OS X.

    Probably I'm missing something but I could't figure out.

    Thanks in advance.
     
    js, Apr 22, 2007
    #1
    1. Advertising

  2. Re: file.read() returns an emtpy even if its currenet position is not at the end

    On Apr 22, 6:51 pm, "js " <> wrote:
    > Hi list.
    >
    > I'm writing a tail -f like program in python
    > and I found file.read() doesn't work as I think it should.
    >
    > Here's the code illustrating my problem.
    >
    > ###
    > #!/usr/bin/env python
    > import os, sys
    > filename = "test.out"
    >
    > f = open(filename, "w+")
    > f.write("Hello")
    > f.flush()
    >
    > f.seek(0, 2)
    >
    > statinfo = os.stat(filename)
    > print "file size: %d" % statinfo.st_size
    > print "position : %d" % f.tell()
    > line = f.read()
    > print "line : [%s]" % line
    >
    > # Writing the same file using another fd
    > f2 = open(filename, "a+")
    > f2.write("World")
    > f2.flush()
    > f2.close()
    >
    > statinfo = os.stat(filename)
    > print "file size: %d" % statinfo.st_size
    > print "position : %d" % f.tell()
    > line = f.read() # read() returns emtpy!! readlines?() works ok
    > ###
    >
    > Running the above, I got the following.
    > ###
    > file size: 5
    > position : 5
    > line : []
    > file size: 10
    > position : 5
    > ###
    >
    > So my question is
    > why the second f.read() returns an emtpy?>From tell() and its st_size I'm sure that file descriptor is not at the EOF
    >
    > and read()'s doc says
    > "An empty string is returned when EOF is encountered immediately".
    > Using readline() or readlines() instead of read() works great though.
    >
    > I'm using Python 2.4.3 on OS X.
    >
    > Probably I'm missing something but I could't figure out.
    >
    > Thanks in advance.


    I've hit into the same issue recently when implementing more or less
    the same thing and found that doing f.seek(f.tell()) on the file
    object when empty strings start to come out allows you to continue
    read()ing after hitting EOF if the file grows again.

    I finally dropped the "hack" and used readline instead since it made
    me a little bit uneasy though...

    Alberto
     
    Alberto Valverde, Apr 22, 2007
    #2
    1. Advertising

  3. js

    js Guest

    Re: file.read() returns an emtpy even if its currenet position is notat the end

    Thank you for reply.

    I've just found the bug report on this.
    http://sourceforge.net/tracker/index.php?func=detail&aid=1523853&group_id=5470&atid=105470

    Nobody seems to be working on this, though.

    On 22 Apr 2007 14:41:29 -0700, Alberto Valverde <> wrote:
    > On Apr 22, 6:51 pm, "js " <> wrote:
    > > Hi list.
    > >
    > > I'm writing a tail -f like program in python
    > > and I found file.read() doesn't work as I think it should.
    > >
    > > Here's the code illustrating my problem.
    > >
    > > ###
    > > #!/usr/bin/env python
    > > import os, sys
    > > filename = "test.out"
    > >
    > > f = open(filename, "w+")
    > > f.write("Hello")
    > > f.flush()
    > >
    > > f.seek(0, 2)
    > >
    > > statinfo = os.stat(filename)
    > > print "file size: %d" % statinfo.st_size
    > > print "position : %d" % f.tell()
    > > line = f.read()
    > > print "line : [%s]" % line
    > >
    > > # Writing the same file using another fd
    > > f2 = open(filename, "a+")
    > > f2.write("World")
    > > f2.flush()
    > > f2.close()
    > >
    > > statinfo = os.stat(filename)
    > > print "file size: %d" % statinfo.st_size
    > > print "position : %d" % f.tell()
    > > line = f.read() # read() returns emtpy!! readlines?() works ok
    > > ###
    > >
    > > Running the above, I got the following.
    > > ###
    > > file size: 5
    > > position : 5
    > > line : []
    > > file size: 10
    > > position : 5
    > > ###
    > >
    > > So my question is
    > > why the second f.read() returns an emtpy?>From tell() and its st_size I'm sure that file descriptor is not at the EOF
    > >
    > > and read()'s doc says
    > > "An empty string is returned when EOF is encountered immediately".
    > > Using readline() or readlines() instead of read() works great though.
    > >
    > > I'm using Python 2.4.3 on OS X.
    > >
    > > Probably I'm missing something but I could't figure out.
    > >
    > > Thanks in advance.

    >
    > I've hit into the same issue recently when implementing more or less
    > the same thing and found that doing f.seek(f.tell()) on the file
    > object when empty strings start to come out allows you to continue
    > read()ing after hitting EOF if the file grows again.
    >
    > I finally dropped the "hack" and used readline instead since it made
    > me a little bit uneasy though...
    >
    > Alberto
    >
    > --
    > http://mail.python.org/mailman/listinfo/python-list
    >
     
    js, Apr 23, 2007
    #3
    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. ad
    Replies:
    1
    Views:
    1,395
    =?Utf-8?B?c2VyZ2UgY2FsZGVyYXJh?=
    Jul 15, 2005
  2. =?Utf-8?B?RGFiYmxlcg==?=

    bridview doesn't appear if table is emtpy

    =?Utf-8?B?RGFiYmxlcg==?=, Mar 19, 2006, in forum: ASP .Net
    Replies:
    2
    Views:
    972
    =?Utf-8?B?RGFiYmxlcg==?=
    Mar 19, 2006
  3. Christoph Dahlen
    Replies:
    2
    Views:
    618
    Christoph Dahlen
    Sep 9, 2006
  4. Jimmy
    Replies:
    3
    Views:
    355
    J. J. Farrell
    Sep 9, 2011
  5. Jimmy
    Replies:
    13
    Views:
    670
    Peter Nilsson
    Sep 21, 2011
Loading...

Share This Page