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:

    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. Advertisements

  2. 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


    Mark Lawrence.
    Mark Lawrence, Aug 16, 2010
    1. Advertisements

  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':
    if datsec:
    if ln==10255:
    print fileinput.filename().rsplit('\\',1)[1]


    Alex van der Spek
    Alex van der Spek, Aug 16, 2010
  4. 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):

    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.
    if word[0] == 'DataID':

    Steven D'Aprano, Aug 17, 2010
  5. 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)
    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.
    Dennis Lee Bieber, Aug 17, 2010
  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
    1. Advertisements

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 (here). After that, you can post your question and our members will help you out.