473,396 Members | 1,997 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

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

js
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.
Apr 22 '07 #1
2 1906
On Apr 22, 6:51 pm, "js " <ebgs...@gmail.comwrote:
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

Apr 22 '07 #2
js
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 <al*****@toscat.netwrote:
On Apr 22, 6:51 pm, "js " <ebgs...@gmail.comwrote:
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
Apr 22 '07 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

6
by: Russell E. Owen | last post by:
At one time, mixing for x in file and readline was dangerous. For example: for line in file: # read some lines from a file, then break nextline = readline() # bad would not do what a naive...
0
by: Lokkju | last post by:
I am pretty much lost here - I am trying to create a managed c++ wrapper for this dll, so that I can use it from c#/vb.net, however, it does not conform to any standard style of coding I have seen....
1
by: Roy | last post by:
Hi, I have a problem that I have been working with for a while. I need to be able from server side (asp.net) to detect that the file i'm streaming down to the client is saved...
14
by: prasadjoshi124 | last post by:
Hi All, I am writing a small tool which is supposed to fill the filesystem to a specified percent. For, that I need to read how much the file system is full in percent, like the output given...
9
by: Use*n*x | last post by:
Hello, I have a binary file (image file) and am reading 4-bytes at a time. The File size is 63,480,320 bytes. My assumption is that if I loop through this file reading 4 bytes at a time, I...
19
by: Lee Crabtree | last post by:
Is there a class in the framework that allows me read text from a file in an unbuffered manner? That is, I'd like to be able to read lines in the same manner as StreamReader.ReadLine(), but I also...
4
by: MikeJ | last post by:
make a While loop ofs = TextFileServer("somefile") string srow while (ofs=false) { srow=ofs.getRow(); Console.Writeline(srow); }
13
by: thomas.mertes | last post by:
Hello Recently I discovered some problem. I have some C code which determines how many bytes are available in a file. Later I use this information to malloc a buffer of the correct size before...
4
by: Ty | last post by:
Hello all, I am creating a web site with Visual Stuido 2008. I am trying to use a java script file to create a busybox for login from this page http://blogs.crsw.com/mark/articles/642.aspx. I...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.