How to check for EOF in a text file

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • nemooo
    New Member
    • Oct 2007
    • 1

    How to check for EOF in a text file

    Hi...This is my first post....I am a newbie to python programming and I have started a program that uses file handling....My problem is that despite the fact that other programming languages (pascal lets say) has an EOF command python doesnt...

    My code is something like that:
    [CODE=python]
    for i in range (99): # this is just an example
    fin.open("text" , "r+")
    fin.seek(i)
    text=fin.read(1 )
    fin.close()
    print fin
    i=i+1[/CODE]


    The problem is that the file is a document and I need to read every letter individually and stop reading at the end of the document.....An y ideas?
    ((Sry if this sound a little bit newbish but i am completely new to that stuff))
    Last edited by bartonc; Oct 14 '07, 09:24 PM. Reason: Added [CODE=python][/CODE] tags.
  • bartonc
    Recognized Expert Expert
    • Sep 2006
    • 6478

    #2
    Originally posted by nemooo
    Hi...This is my first post....I am a newbie to python programming and I have started a program that uses file handling....My problem is that despite the fact that other programming languages (pascal lets say) has an EOF command python doesnt...

    My code is something like that:
    [CODE=python]
    for i in range (99): # this is just an example
    fin.open("text" , "r+")
    fin.seek(i)
    text=fin.read(1 )
    fin.close()
    print fin
    i=i+1[/CODE]


    The problem is that the file is a document and I need to read every letter individually and stop reading at the end of the document.....An y ideas?
    ((Sry if this sound a little bit newbish but i am completely new to that stuff))
    We generally don't use read() very often in Python. Files are much easier to handle by iterating over them as if they are lists. The docs that that read() retruns and empty string when EOF is encountered:
    read( [size])

    Read at most size bytes from the file (less if the read hits EOF before obtaining size bytes). If the size argument is negative or omitted, read all data until EOF is reached. The bytes are returned as a string object. An empty string is returned when EOF is encountered immediately.
    So:[CODE=python]f = open(r'D:\My Documents\Pytho n\TheScripts examples\temp.t xt')
    while True:
    c = f.read(1)
    if not c:
    break
    print c,

    f.close()

    t e s t i n g[/CODE]

    Comment

    • Smygis
      New Member
      • Jun 2007
      • 126

      #3
      I wuld do it.

      [code=python]
      for c in open("testfile" ).read():
      print c,
      [/code]

      Originally posted by nemooo
      The problem is that the file is a document and I need to read every letter individually and stop reading at the end of the document.....An y ideas?
      ((Sry if this sound a little bit newbish but i am completely new to that stuff))
      Its way better to read in the whole file at once and then process it.

      Comment

      • bartonc
        Recognized Expert Expert
        • Sep 2006
        • 6478

        #4
        Originally posted by Smygis
        I wuld do it.

        [code=python]
        for c in open("testfile" ).read():
        print c,
        [/code]



        Its way better to read in the whole file at once and then process it.
        That's true of small files. On large files you may only get a buffer's worth on a single read().

        Comment

        Working...