Walking up/down directory trees in Python

by Amit

You can use the os.walk in Python to traverse a directory tree:

os.walk(top[, topdown=True[, onerror=None[, followlinks=False]]])

For example:


import tarfile
import os

for dirpath, dirnames, filenames in os.walk("/tmp"):
print dirnames



The output can be best understood when co-related with the o/p of tree.

Usage: A simple Python script to create a tar archive out of a directory is here