Using os.walk() to recursively traverse directories in Python

I want to navigate from the root directory to all other directories within and print the same.

Here's my code:


    import os
    import fnmatch

    for root, dir, files in os.walk("."):
            print root
            print ""
            for items in fnmatch.filter(files, "*"):
                    print "..." + items
            print ""

And here's my O/P:




Above, . and ./packagedemo are directories.

However, I need to print the O/P in the following manner:


Above, A and B are directories and the rest are files.

This will give you the desired result


    import os

    # traverse root directory, and list directories as dirs and files as files
    for root, dirs, files in os.walk("."):
        path = root.split(os.sep)
        print((len(path) - 1) * '---', os.path.basename(root))
        for file in files:
            print(len(path) * '---', file)