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?
 
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)

--
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top