newb question: file searching

godavemon davefowler at gmail.com
Tue Aug 8 16:08:40 EDT 2006


Hey, I've done similar things.

You can use system comands with the following

import os
os.system('command here')

You'd maybe want to do something like

dir_name = 'mydirectory'
import os
os.system('ls ' +dir_name + ' > lsoutput.tmp')
fin = open('lsoutput.tmp', 'r')
file_list = fin.readlines()
fin.close()

Now you have a list of all the files in dir_name stored in file_list.

Then you'll have to parse the input with string methods.  They're easy
in python.  Here's the list of them:
http://docs.python.org/lib/string-methods.html

There is probably a better way to get the data from an os.system
command but i haven't figured it out.  Instead what i'm doing is
writing the stdio output to a file and reading in the data.  It works
fine.  Put it in your tmp dir if you're in linux.


jaysherby at gmail.com wrote:
> I'm new at Python and I need a little advice.  Part of the script I'm
> trying to write needs to be aware of all the files of a certain
> extension in the script's path and all sub-directories.  Can someone
> set me on the right path to what modules and calls to use to do that?
> You'd think that it would be a fairly simple proposition, but I can't
> find examples anywhere.  Thanks.




More information about the Python-list mailing list