[Tutor] Filter question

Kooser, Ara S askoose at sandia.gov
Tue Apr 13 15:50:33 EDT 2004


Thank you for the reply. I first tried just a simple copy from with the same
opening code and I had no problem. When I added the filter statement then it
stopped working. I will go read up more on the folterFile. I guess I am
entering something wrong. Here is the copy code I used.

inp = open("lmps.txt","r")
outp = open("out.txt","w")
for line in inp.readlines():
    outp.write(line)
print "1 file copied"
inp.close()
outp.close()


-----Original Message-----
From: Danny Yoo [mailto:dyoo at hkn.eecs.berkeley.edu] 
Sent: Tuesday, April 13, 2004 1:32 PM
To: Kooser, Ara S
Cc: 'tutor at python.org'
Subject: Re: [Tutor] Filter question




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

Hi Ara,

There's a bug in the file opening code:

###
def filterFile(lmps, out):
    inp = open("lmps","r")
    outp = open("out","w")
    ...
###

The code, as written, is trying to open up a file that's literally named
'Imps' for reading.  It's also trying to open up a file that is literally
called 'out'.

Hope this helps!





More information about the Tutor mailing list