Walking up/down directory trees in Python
You can use the os.walk
in Python to traverse a directory tree:
os.walk(top[, topdown=True[, onerror=None[, followlinks=False]]])
For example:
#!/usr/bin/python
import tarfile
import os
for dirpath, dirnames, filenames in os.walk("/tmp"):
print dirnames
gives:
/tmp
/tmp/.vbox-amit-ipc
/tmp/ssh-zYrvo4322
/tmp/.ICE-unix
/tmp/kde-amit
/tmp/plugtmp-1
/tmp/acroread_1000_100
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