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