Mailing List Archive

applying a filter to all files in a directory
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.

thanks for any help,
tom

Tomasz_Lukasiak@Brown.EDU

-----------== Posted via Deja News, The Discussion Network ==----------
http://www.dejanews.com/ Search, Read, Discuss, or Start Your Own
applying a filter to all files in a directory [ In reply to ]
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.

thanks for any help,
tom

Tomasz_Lukasiak@Brown.EDU


-----------== Posted via Deja News, The Discussion Network ==----------
http://www.dejanews.com/ Search, Read, Discuss, or Start Your Own
applying a filter to all files in a directory [ In reply to ]
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.

thanks for any help,
tom

Tomasz_Lukasiak@Brown.EDU
applying a filter to all files in a directory [ In reply to ]
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@zarlut.utexas.edu
Remove the z from this address to reply.
Stop spam! http://spam.abuse.net/spam/
applying a filter to all files in a directory [ In reply to ]
Tomasz Lukasiak <Tomasz_Lukasiak@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>
applying a filter to all files in a directory [ In reply to ]
tlukasiak@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@Brown.EDU

--

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