applying a filter to all files in a directory

Fredrik Lundh fredrik at pythonware.com
Mon May 3 04:28:06 EDT 1999


Tomasz Lukasiak <Tomasz_Lukasiak at Brown.EDU> wrote:
>     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.

import os, sys
for file in filter(os.path.isfile, os.listdir(sys.argv[1])):
    process(file)

to process text files, you can pass the file list to
fileinput.input instead, and use the inplace option.
see the fileinput docs for details.

</F>





More information about the Python-list mailing list