Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

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.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7787543
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T20:42:20+00:00 2026-06-01T20:42:20+00:00

fp = open(a.txt) #do many things with fp c = fp.read() if c is

  • 0
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?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-01T20:42:22+00:00Added an answer on June 1, 2026 at 8:42 pm

    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 when read returns less than the number of bytes you requested. In that case, the following read call will return the empty string (not None). The following loop reads a file in chunks; it will call read at most once too many.

    assert n > 0
    while True:
        chunk = fp.read(n)
        if chunk == '':
            break
        process(chunk)
    

    Or, shorter:

    for chunk in iter(lambda: fp.read(n), ''):
        process(chunk)
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm not sure if it's just me, but whenever I open .txt files in
f = open('wl4.txt', 'w') hh = 0 ###################################### for n in range(1,5): for l
When I do open('output1.txt', 'w').write(Hello guys) A file called output1.txt is immediatly created and
When running the following python code: >>> f = open(rmyfile.txt, a+) >>> f.seek(-1,2) >>>
properties = [color, font-size, font-family, width, height] inPath = style.css outPath = output.txt #Open
I would like to open 2 url's using open $url = 'http://xtreme-jumps.eu/demos.txt'; $url2 =
I tried this code to open a file in Python: f = open("/Desktop/temp/myfile.txt","file1") It
The code chokes at fopen(): <?php ini_set('display_errors',1); error_reporting(E_ALL); $fp = fopen(/path/to/file/some_file.txt,a) or die(can't open
Are there any R users aware of an "open file"-type function in R? Preferably
I read many files from my system. I want to read them faster, maybe

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.