Programming and writing about it.

echo $RANDOM

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

Advertisement

Book Chapter Review: (Python) Package Management

Chapter 9 of the book Python for Unix and Linux System Administration, 1st Edition discusses package management in the Python programming language.

When you read the chapter, you get a feeling that you are reading about package management on your favorite Linux distro, which IMO is a good thing. I, personally love to co-relate the new things I learn with things I already know.

It starts off with Python’s own apt-get (or zypper or yum): easy_install. Then it moves on to Python eggs, and how simple it is to create them.

You have written a Python package and you want to upload it to the Python package index. The book shows you how.

The chapter also looks at Buildout, and virtualenv (which helps set up more than one ‘virtual’ Python environments on your system, like chroot does)

Update: The book also talks about ESP Package Manager (EPM)