applying a filter to all files in a directory

Magnus L. Hetland mlh at idt.ntnu.no
Mon May 3 08:30:24 EDT 1999


tlukasiak at my-dejanews.com writes:

> hi,
> 
>    i have a question to which i'm sure there is a simple answer, but i
> couldn't find any references.  i've written a program that converts a
> file from one format to another.  now, i'd like to write a python script
> that, when given a directory name, will apply this converter program to
> all the files in the specified directory.  what is the best way to do
> this?  i'm using a UNIX machine, but am unsure how to use the Python
> file manipulation libraries.

Well -- first of all, using fileimport with the inplace flag set is
nice for changing files like this... But since you have already made
your filter, what you probably need is simply something like
os.listdir -- and then use a for loop to iterate over the files.
However, if you use fileinput, you could simply call your script like
this:

$ python filter.py *

and all the files in the current directory would be filtered.
(Fileinput is quite practical...)

> 
> thanks for any help,
> tom
> 
> Tomasz_Lukasiak at Brown.EDU

--

  Magnus
  Lie
  Hetland        http://arcadia.laiv.org <arcadia at laiv.org>




More information about the Python-list mailing list