[Tutor] Filtering text files

Brian van den Broek bvande at po-box.mcgill.ca
Wed Apr 14 18:28:40 EDT 2004

Kooser, Ara S said unto the world upon 14/04/2004 17:28:

> Hello,
>    I posted several question about filtering text files. I am still a little
> confused. I ran through some of the tutorials on python.org and I am still
> getting hung up. I am running python 2.3.3. When I try and run the program
> below I receive the prompt on the shell and no new file is created in my
> directory. Here is the code from
> http://www.ibiblio.org/obp/thinkCSpy/chap11.htm
> <http://www.ibiblio.org/obp/thinkCSpy/chap11.htm> . I have in my directory a
> file named lmps for the input file. Also do the names in the function have
> to be the same as the names of the files being loaded? I have tried both.
> Thanks
> Ara
> def filterFile(lmps, newFile):
>   f1 = open(lmps, "r")
>   f2 = open(newFile, "w")
>   while 1:
>     text = f1.readline()
>     if text == "":
>       break
>     if text[0] == 'I':
>       continue
>     f2.write(text)
>   f1.close()
>   f2.close()
>   return 


Use caution before assuming that what follows is true in its
entirety. I am a newbie too!

Did you post the complete code that you are running? If so, the
problem is that you have defined a function and then stopped. You
need to call the function too, in order for it to do any work.
(Think of the function definition as a list of instructions that
can be done in one fell swoop after they have been defined. But a
list of instructions doesn't do anything by itself. For stuff to
happen, they need to be followed.)

The 'lmps' and 'newFile' in the function definition are the names
internal to the function. A function call of filterFile as the
code above defines it needs two arguments, one which plays the
role the function gives to lmps, the other newFile. So, to use the
function to read firstfile.txt and write the contents to
secondfile.txt you need to call the function like this:

filterFile(firstfile.txt, secondfile.txt)

The two arguments in the filterFile function call can be any files
whatsoever. You could even read a file called newFile and write to
a file called lmps by calling the function like so:

filterFile(newFile, lmps)

This might seem confusing, because the two arguments are in the
opposite order of the function definition. But the names used in
the function definition are arbitrary placeholders (you could just
as well define with 'first_argument', 'second_argument' as the
names in the definition). They serve to define the function by
keeping the role of each argument straight. They need not have any
relation to the actual arguments used when you call the function

So, to make it work, follow the function definition with a
function call like I gave as an example, and it should work as you

Two other things:

1) The claim that the argument names are completely arbitrary
holds true for simple function definitions, but the ability to
define keyword arguments adds a bit of complication. I'd say worry
about that a bit later, though -- or at least get an explanation
from someone who knows better than I what they are talking about ;-)

2) The function calls that I gave did not provide a file path.
Thus, they will work as given only if the file specified in the
first argument lives in the current working directory.


Brian vdB

More information about the Tutor mailing list