A question

Will Ware wware-nospam at world.std.com
Sun Oct 17 14:56:52 EDT 1999


Kristian Nylund (krnylund at io.yok.utu.fi) wrote:
: Could someone tell me an easy way to do  
: something like "for i in `ls *.txt`..." 
: in python?

Other folks have mentioned glob.glob(), which will work fine for
things like "*.txt". If you want something more flexible, for
instance all the *.c and *.h files that mention printf,
you'd want to iterate on the output of something like

	find . -name '*.[ch]' -exec grep -l printf {} \;

and if you wanted to do something like that in Python, you'd want
to know about commands.getoutput(). Specifically, you'd want to
write something like this:

	import commands

	for file in commands.getoutput(
		"find . -name '*.[ch]' -exec grep -l printf {} \;"):
	    process(file)

So commands.getoutput() is good to know about if you need a little
more flexibility than glob.glob().

-- 
 - - - - - - - - - - - - - - - - - - - - - - - -
Resistance is futile. Capacitance is efficacious.
Will Ware	email:    wware @ world.std.com




More information about the Python-list mailing list