fp = open("a.txt")
#do many things with fp
c = fp.read()
if c is None:
print 'fp is at the eof'
Besides the above method, any other way to find out whether is fp is already at the eof?
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
fp.read()reads up to the end of the file, so after it’s successfully finished you know the file is at EOF; there’s no need to check. If it cannot reach EOF it will raise an exception.When reading a file in chunks rather than with
read(), you know you’ve hit EOF whenreadreturns less than the number of bytes you requested. In that case, the followingreadcall will return the empty string (notNone). The following loop reads a file in chunks; it will callreadat most once too many.Or, shorter: