Liza
Liza

Reputation: 123

directory listing in python

I am getting trouble with directory listing.Suppose, I have a directory with some subdirectory(named as a-z, 0-9, %, -).In each subdirectory, I have some related xml files. So, I have to read each lines of this files.I have tried with the following code.

def listFilesMain(dirpath):
    for dirname, dirnames, filenames in os.walk(dirpath):
        for subdirname in dirnames:
            os.path.join(dirname, subdirname)
        for filename in filenames:
            fPath = os.path.join(dirname, filename)
            fileListMain.append(fPath)

It works only if I tried to run my program from subdirectory, but no results if I tried to run from main directory. What's going wrong here? Any kind of help will be greatly appreciated. Thanks!

Upvotes: 0

Views: 2428

Answers (2)

Mike DeSimone
Mike DeSimone

Reputation: 42825

How about this:

def list_files(dirpath):
    files = []
    for dirname, dirnames, filenames in os.walk(dirpath):
        files += [os.path.join(dirname, filename) for filename in filenames]
    return files

You could also do this as a generator, so the list isn't stored in its entirety:

def list_files(dirpath):
    for dirname, dirnames, filenames in os.walk(dirpath):
        for filename in filenames:
            yield os.path.join(dirname, filename)

Finally, you might want to enforce absolute paths:

def list_files(dirpath):
    dirpath = os.path.abspath(dirpath)
    for dirname, dirnames, filenames in os.walk(dirpath):
        for filename in filenames:
            yield os.path.join(dirname, filename)

All of these can be called with a line like:

for filePath in list_files(dirpath):
    # Check that the file is an XML file.
    # Then handle the file.

Upvotes: 2

Andre Holzner
Andre Holzner

Reputation: 18695

if your subdirectories are softlinks, make sure you specify followlinks=True as an argument to os.walk(..). From the documentation:

    By default, os.walk does not follow symbolic links to subdirectories on
    systems that support them.  In order to get this functionality, set the
    optional argument 'followlinks' to true.

Upvotes: 1

Related Questions