EOFError not getting raised


D

David Bear

I have been trying to do something like this:

f = open('someDocs.str', 'r')
try:
while True:
ln = f.readline()
except EOFError:
print 'reached eof'
f.close()
sys.exit(1)


However, EOFError is never raised. What am I missing?
 
Ad

Advertisements

D

Dennis Lee Bieber

However, EOFError is never raised. What am I missing?

The Python Library Reference, perhaps?

-=-=-=-=-=-=-=- PLR
readline( [size])
Read one entire line from the file. A trailing newline character is kept
in the string2.11 (but may be absent when a file ends with an incomplete
line). If the size argument is present and non-negative, it is a maximum
byte count (including the trailing newline) and an incomplete line may
be returned. An empty string is returned only when EOF is encountered
immediately. Note: Unlike stdio's fgets(), the returned string contains
null characters ('\0') if they occurred in the input.
-=-=-=-=-=-=-=-

.readline() does NOT raise EOF.

f = open('someDocs.str', 'r')
while True:
ln = f.readline()
if not ln: break
print 'reached eof'
f.close()
sys.exit(1)

--
 
Ad

Advertisements


Top