applying a filter to all files in a directory
Tom Bryan
tbryan at zarlut.utexas.edu
Sun May 2 22:12:06 EDT 1999
Tomasz Lukasiak 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.
Assuming you mean all of the files in that directory or its
subdirectories...
>From outside of Python, something like this should work.
$ find directory-name -type f -exec converter.py \{\} \;
>From within Python, it sounds like you might want to use os.path.walk.
Python 1.5.1 (#3, Mar 7 1999, 20:11:49) [GCC 2.7.2] on sunos5
Copyright 1991-1995 Stichting Mathematisch Centrum, Amsterdam
>>> import os
>>> print os.path.walk.__doc__
walk(top,func,args) calls func(arg, d, files) for each directory "d"
in the tree rooted at "top" (including "top" itself). "files" is a
list of all the files and subdirs in directory "d".
Otherwise, you might want to do something like os.listdir from
an importing module and call the function manually in a loop.
--
tbryan at zarlut.utexas.edu
Remove the z from this address to reply.
Stop spam! http://spam.abuse.net/spam/
More information about the Python-list
mailing list