Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Python > EOFError with fileinput

Reply
Thread Tools

EOFError with fileinput

 
 
Alex van der Spek
Guest
Posts: n/a
 
      08-16-2010
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

 
Reply With Quote
 
 
 
 
Mark Lawrence
Guest
Posts: n/a
 
      08-16-2010
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.

 
Reply With Quote
 
 
 
 
Alex van der Spek
Guest
Posts: n/a
 
      08-16-2010
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" <(E-Mail Removed)> wrote in message
news:4c696751$0$22940$(E-Mail Removed)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


 
Reply With Quote
 
Steven D'Aprano
Guest
Posts: n/a
 
      08-16-2010
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
 
Reply With Quote
 
Dennis Lee Bieber
Guest
Posts: n/a
 
      08-17-2010
On Mon, 16 Aug 2010 18:29:06 +0200, "Alex van der Spek"
<(E-Mail Removed)> 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://www.velocityreviews.com/forums/(E-Mail Removed) HTTP://wlfraed.home.netcom.com/

 
Reply With Quote
 
Alex van der Spek
Guest
Posts: n/a
 
      08-17-2010
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

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
EOFError: EOF when reading a line Mihir Patel Python 1 03-12-2010 01:22 AM
ftplib returns EOFError Jon Bowlas Python 0 05-19-2008 11:27 AM
Exception EOFError. Dr. Pastor Python 3 09-04-2006 07:35 AM
Mysterious EOFError Rex Eastbourne Python 3 04-09-2006 10:23 PM
EOFError not getting raised David Bear Python 2 06-10-2005 11:44 AM



Advertisments