EOFError with fileinput

A

Alex van der Spek

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
 
M

Mark Lawrence

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

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

Alex van der Spek

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
 
S

Steven D'Aprano

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()
 
D

Dennis Lee Bieber

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

Alex van der Spek

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
 

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

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top