EOFError with fileinput

Discussion in 'Python' started by Alex van der Spek, Aug 16, 2010.

  1. Using the fileinput module to process lists of files:

    >>>for line in fileinput.input(logs):


    Unfortunately, EOFError does not seem to indicate the end-of-file condition
    correctly when using fileinput.

    How would you find the EOF file for all the files in the file list 'logs'?

    Thank you,
    Alex van der Spek
     
    Alex van der Spek, Aug 16, 2010
    #1
    1. Advertising

  2. On 16/08/2010 17:29, Alex van der Spek wrote:
    > Using the fileinput module to process lists of files:
    >
    >>>> for line in fileinput.input(logs):

    >
    > Unfortunately, EOFError does not seem to indicate the end-of-file
    > condition correctly when using fileinput.
    >
    > How would you find the EOF file for all the files in the file list 'logs'?
    >
    > Thank you,
    > Alex van der Spek


    I'm not sure exactly what you're asking here. Could you a brief
    description as to what you're trying to achieve, then put the question
    again.

    Cheers.

    Mark Lawrence.
     
    Mark Lawrence, Aug 16, 2010
    #2
    1. Advertising

  3. Here is an excerpt. It works because the end condition is a fixed number
    (ln==10255), the approximate number of data lines in a file. If I replace
    that condition by EOFError, the program does not do the intended work. It
    appears as if EOFError is always true in this case.

    +++++++++++++++++++++++++++++++++++++++++++

    for line in fileinput.input(logs):
    if line.split()[0]=='DataID':
    datsec=True
    ln=0
    if datsec:
    lines[fn].append(line.split())
    ln+=1
    if ln==10255:
    datsec=False
    fileinput.nextfile()
    fn+=1
    print fileinput.filename().rsplit('\\',1)[1]
    fileinput.close()

    +++++++++++++++++++++++++++++++++++++++++++

    Regards,
    Alex van der Spek


    "Alex van der Spek" <> wrote in message
    news:4c696751$0$22940$4all.nl...
    > Using the fileinput module to process lists of files:
    >
    >>>>for line in fileinput.input(logs):

    >
    > Unfortunately, EOFError does not seem to indicate the end-of-file
    > condition correctly when using fileinput.
    >
    > How would you find the EOF file for all the files in the file list 'logs'?
    >
    > Thank you,
    > Alex van der Spek
     
    Alex van der Spek, Aug 16, 2010
    #3
  4. On Mon, 16 Aug 2010 19:27:32 +0200, Alex van der Spek wrote:

    > Here is an excerpt. It works because the end condition is a fixed number
    > (ln==10255), the approximate number of data lines in a file. If I
    > replace that condition by EOFError, the program does not do the intended
    > work. It appears as if EOFError is always true in this case.


    I don't understand what you mean by "replace that condition by EOFError".
    I can only guess you mean what you say literally, and replace the test:

    if ln==10255:

    with the test:

    if EOFError:


    If not, then I have no idea what you mean. But if that is what you're
    doing, then you're doing it completely wrong! Exceptions aren't flags
    that get set globally by magic. You have completely misunderstood
    Python's programming model.

    In this case, the point of using fileinput is so you don't need to care
    about the EOF of each file. fileinput takes care of that for you,
    automatically detecting the end of file and switching to the next file as
    needed. You only need to call fileinput.nextfile() if you want to switch
    to the next file *before* reaching end-of-file.

    The usual way to use fileinput is something like:

    import fileinput
    for line in fileinput.input(list_of_filenames):
    process(line)

    which will process all the lines in each file in turn.

    I'm not entirely sure what you are attempting to do in your example code.
    It doesn't help that you are using variable names like fn and ln. But I
    will take a guess. Here's my attempt to re-write and simplify it, by
    changing the condition from "is this the last line?" to "is this the
    first line?".



    import fileinput
    import os

    file_number = -1
    lines = [ [] for i in len(logs)) ]
    for line in fileinput.input(logs):
    if fileinput.isfirstline():
    file_number += 1
    pathname, filename = os.path.split(fileinput.filename())
    print filename
    words = line.split()
    if not words:
    # Line was blank (just whitespace) and can be ignored.
    continue
    if word[0] == 'DataID':
    lines[file_number].append(words)

    fileinput.close()



    --
    Steven
     
    Steven D'Aprano, Aug 17, 2010
    #4
  5. On Mon, 16 Aug 2010 18:29:06 +0200, "Alex van der Spek"
    <> declaimed the following in gmane.comp.python.general:

    > Using the fileinput module to process lists of files:
    >
    > >>>for line in fileinput.input(logs):

    >
    > Unfortunately, EOFError does not seem to indicate the end-of-file condition
    > correctly when using fileinput.
    >

    I would hope not... EOFError is an exception, as I recall, and the
    whole purpose of fileinput is to silently process the list of files,
    automatically switching to the next file when EOF is reached on the
    current one.

    Oh... BTW; EOFError is only raised by input() and raw_input()...
    Normal file I/O signals EOF by returning an empty string (which is not
    the same as a string with a space or a newline and nothing else)

    > How would you find the EOF file for all the files in the file list 'logs'?
    >

    If you examine the documentation for fileinput, you'll find that it
    has a method that returns true when the FIRST line of a file has been
    read; along with method to return the file name of the current file.

    --
    Wulfraed Dennis Lee Bieber AF6VN
    HTTP://wlfraed.home.netcom.com/
     
    Dennis Lee Bieber, Aug 17, 2010
    #5
  6. Thanks all! I understand better now. I had no idea that EOFError was an
    exception. I was looking for a function to tell me when the end of a
    sequential file is reached as in all of the 4 programming languages that I
    do know this is a requirement.

    Will modify my program accordingly.

    Alex van der Spek
     
    Alex van der Spek, Aug 17, 2010
    #6
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. David Bear

    EOFError not getting raised

    David Bear, Jun 10, 2005, in forum: Python
    Replies:
    2
    Views:
    414
  2. Rex Eastbourne

    Mysterious EOFError

    Rex Eastbourne, Apr 9, 2006, in forum: Python
    Replies:
    3
    Views:
    962
    Dennis Lee Bieber
    Apr 9, 2006
  3. Dr. Pastor

    Exception EOFError.

    Dr. Pastor, Sep 4, 2006, in forum: Python
    Replies:
    3
    Views:
    442
    Sybren Stuvel
    Sep 4, 2006
  4. Jon Bowlas

    ftplib returns EOFError

    Jon Bowlas, May 19, 2008, in forum: Python
    Replies:
    0
    Views:
    301
    Jon Bowlas
    May 19, 2008
  5. Mihir Patel

    EOFError: EOF when reading a line

    Mihir Patel, Mar 11, 2010, in forum: Python
    Replies:
    1
    Views:
    1,773
    Gabriel Genellina
    Mar 12, 2010
Loading...

Share This Page