[Tutor] Filter question

denis denis.spir at free.fr
Tue Apr 13 16:24:02 EDT 2004


Two comments :
You try to open files using (between quotes) the names of the function
parameters, while they already are strings. You shoulg get an error, unless
there actually is in your current directory a file called "lmps". Try
without quotes.
The way you list the lines of your input files is a bit hard. There are more
readable loop structures for that, like (not tested) :
***
lines = inp.readlines()
for l in lines:
    if l[0] == 'L' : outp.write(l)
***
You don't need to 'wildly' test for the eof.

denis

----- Original Message -----
From: Kooser, Ara S <askoose at sandia.gov>
To: <tutor at python.org>
Sent: Tuesday, April 13, 2004 9:11 PM
Subject: [Tutor] Filter question


> Hello,
>
>    I am new to python. I am trying to covert one file type to another
> (molecular dynamics output to visualizer input). I have set up the
following
> filter, but it is not working. I don't receive an error message just a
blank
> prompt at shell. Here is the code. I am trying to filter out any line the
> begins with I. I am using python 2.3.3. Thanks.
>
> Ara
>
> def filterFile(lmps, out):
>     inp = open("lmps","r")
>     outp = open("out","w")
>     while 1:
>         text = inp.readline()
>         if line == "":
>             break
>         if line[0] == 'I':
>             continue
>         outp.write(line)
>
>     inp.close()
>     outp.close()
>


----------------------------------------------------------------------------
----


> _______________________________________________
> Tutor maillist  -  Tutor at python.org
> http://mail.python.org/mailman/listinfo/tutor
>




More information about the Tutor mailing list