Home > python > Traversing a directory recursively

Traversing a directory recursively


You want to traverse a directory recursively.

Solution #1

#!/usr/bin/env python

import os

def processDirectory ( args, dirname, filenames ):
    print dirname
    for filename in filenames:
        print " " * 4 + filename

base_dir = "."
os.path.walk( base_dir, processDirectory, None )

os.path.walk() works with a callback: processDirectory() will be called for each directory encountered.

Sample output with base_dir = '/etc':


Solution #2, manual method (update at 20110509)

#!/usr/bin/env python

import os
import sys

symlinks = 0

def skip_symlink(entry):
    """Symlinks are skipped."""
    global symlinks
    symlinks += 1
    print "# skip symlink {0}".format(entry)

def process_dir(d, depth):
    print d, "[DIR]"

def process_file(f, depth):
    if depth > 0:
        print ' ' * 4, 
    print f

def traverse(directory, depth=0):
    """Traverse directory recursively. Symlinks are skipped."""
    #content = [os.path.abspath(os.path.join(directory, x)) for x in os.listdir(directory)]
        content = [os.path.join(directory, x) for x in os.listdir(directory)]
    except OSError:
        print >>sys.stderr, "# problem with {0}".format(directory)

    dirs = sorted([x for x in content if os.path.isdir(x)])
    files = sorted([x for x in content if os.path.isfile(x)])

    for d in dirs:
        if os.path.islink(d):
        # else
        dir_name = os.path.split(d)[1]
        process_dir(d, depth)
        traverse(d, depth + 1)
    for f in files:
        if os.path.islink(f):
        # else
        process_file(f, depth)

def main():
    start_dir = '.'
    print "# skipped symlinks: {0}".format(symlinks)


if __name__ == "__main__":

Solution #3 (update at 20130705)

import os
import sys

for root, _, files in os.walk(sys.argv[1]):
    for f in files:
        fname = os.path.join(root, f)
        print fname
        # Remove *.pyc files, compress images, count lines of code
        # calculate folder size, check for repeated files, etc.
        # A lot of nice things can be done here
        # credits: m_tayseer @reddit

Leave a Reply

Please log in using one of these methods to post your comment:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

%d bloggers like this: