Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Python > file.read() returns an emtpy even if its currenet position is not atthe end

Reply
Thread Tools

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

 
 
js
Guest
Posts: n/a
 
      04-22-2007
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.
 
Reply With Quote
 
 
 
 
Alberto Valverde
Guest
Posts: n/a
 
      04-22-2007
On Apr 22, 6:51 pm, "js " <ebgs...@gmail.com> 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

 
Reply With Quote
 
 
 
 
js
Guest
Posts: n/a
 
      04-22-2007
Thank you for reply.

I've just found the bug report on this.
http://sourceforge.net/tracker/index...70&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 " <ebgs...@gmail.com> 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
>

 
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
Is a blank at the end of a C file a better style than a newline atthe end of the file or is it better to place none? Jimmy C Programming 13 09-21-2011 12:17 AM
Is a blank at the end of a C file a better style than a newline atthe end of the file or is it better to place none? Jimmy C Programming 3 09-09-2011 10:36 PM
farming automation [parallel testing on multipe remote machines atthe same time] Cristina Ruby 2 07-16-2010 02:52 AM
[Python 2.6] print_function and unicode_literals cannot be used atthe same time? ÖܼÃÊÇĸÀÏÊó Python 3 10-29-2008 03:07 AM
Notepad .its faulty ? its not the be and and end all ! Homeworker Computer Support 2 04-07-2007 05:47 PM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57