By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
444,120 Members | 1,799 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 444,120 IT Pros & Developers. It's quick & easy.

File.read() sometimes not reading everything

P: n/a
HI all,

I came across a strange behaviour with read method, and I'm not sure if
this is a filesystem problem or a misuse of this method.
I do the simple following thing :
f = open ('myFile','r')
content = f.read()
f.close()

When I use this lines really intensively sometimes, at random the content
is not the full content, it lacks some lines at the end.
When reading the file.read docstring I see that there can be some problems
with non-blocking file, but I suppose open() is by default creating a blocking
file.

Is the file line-buffered by default ? This could be a reason for this
early EOF, as I'm reading all the lines at once, and there is several
lines in the file ?

I'm at the moment trying to replace the previous read call by:
content = '\n'.join(f.readlines)
Not sure if this will work better, I'll say you.

Any other idea of this strange behaviour ? Can this be a problem with
filesystem also ?

Any ideas welcomed !

Boris
Jul 18 '05 #1
Share this Question
Share on Google+
2 Replies


P: n/a
Your file may contain an EOF character before the actual end
of file. You might want to try f=open('myFile','rb') to
open in binary mode, which would ignore EOF characters and
read the entire file.

-Larry
"Boris Boutillier" <bo**************@arteris.net> wrote in message
news:pa****************************@arteris.net...
HI all,

I came across a strange behaviour with read method, and I'm not sure if
this is a filesystem problem or a misuse of this method.
I do the simple following thing :
f = open ('myFile','r')
content = f.read()
f.close()

When I use this lines really intensively sometimes, at random the content
is not the full content, it lacks some lines at the end.
When reading the file.read docstring I see that there can be some problems
with non-blocking file, but I suppose open() is by default creating a blocking file.

Is the file line-buffered by default ? This could be a reason for this
early EOF, as I'm reading all the lines at once, and there is several
lines in the file ?

I'm at the moment trying to replace the previous read call by:
content = '\n'.join(f.readlines)
Not sure if this will work better, I'll say you.

Any other idea of this strange behaviour ? Can this be a problem with
filesystem also ?

Any ideas welcomed !

Boris

Jul 18 '05 #2

P: n/a
In article <pa****************************@arteris.net>,
"Boris Boutillier" <bo**************@arteris.net> wrote:
I came across a strange behaviour with read method, and I'm not sure if
this is a filesystem problem or a misuse of this method.
I do the simple following thing :
f = open ('myFile','r')
content = f.read()
f.close()

When I use this lines really intensively sometimes, at random the content
is not the full content, it lacks some lines at the end.
When reading the file.read docstring I see that there can be some problems
with non-blocking file, but I suppose open() is by default creating a blocking
file.

Is the file line-buffered by default ? This could be a reason for this
early EOF, as I'm reading all the lines at once, and there is several
lines in the file ?


No, a disk file will be block buffered. Not that it would matter.
Basically, the read method calculates the size of the file and tries
to fread(3) that amount. The only explanation I can think of (other
than an error somewhere) is a concurrent change to the file.

Donn Cave, do**@u.washington.edu
Jul 18 '05 #3

This discussion thread is closed

Replies have been disabled for this discussion.