On Sat, 15 Jan 2005 15:16:02 GMT, .removethis. <"(.removethis.)kartic.krishnamurthy"@gmail.com> wrote:

Sara Fwd said the following on 1/15/2005 8:10 AM:
Can you guys also help me find a module that looks in
a directory and print out the names of the files in there?

You can use glob:

 >>> import glob
 >>> from os.path import isfile
 >>> print filter(isfile, glob.glob('/tmp/*')) # can use patterns

(will print a list of all files in the given directory, matching the given pattern)

If you want to traverse a directory tree recursively, please take a look at this recipe: http://aspn.activestate.com/ASPN/Cookbook/Python/Recipe/200131

import os, os.path

def get_all_files(path):
    if len(path) > 0:
        if path[-1] == ':':
            path = path + '\\'
    try:
        for i in os.listdir(path):
            j = os.path.join(path, i)
            if os.path.isdir(j):
                for ii in get_all_files(j):
                    yield ii
            else:
                yield j
    except:pass

for i in get_all_files('c:\\'):
    print i
--
http://mail.python.org/mailman/listinfo/python-list

Reply via email to