The most fundamental of abilities in shell scripts is moving about the file system.

import os
os.chdir('/')
contents = os.listdir(os.getcwd())

To change directories, we use os.chdir with an argument of our destination. To get a listing of the contents of a directory, os.listdir is the command to use. It takes one argument, which is the directory that holds the contents. If we want to know our current location on the filesystem, then os.getcwd will tell us the path.

Instead of moving around, you may want to do the same thing on the files and folders of a tree of directories, a.k.a. a recursive operation. In this next scenario, I have a hypothetical module called mp32vorbis that has a convert function, which converts a specified mp3 file into an ogg vorbis file.

import os
import mp32vorbis

for root, dirs, files in os.walk(os.getcwd()):
    for dir in dirs:
        print "Processing", os.path.join(root, dir)
    for file in files:
        if file.endswith('.mp3'):
            print "Converting", file
            mp32vorbis.convert(file)

As you can see, recursive operations are quick and easy with os.walk. It is a generator that yields each directory, root, plus a collection of directories and files within it. You can process each separate directory and file by looping through them. You may have noticed os.path.join. This is a simple function that connects its arguments with the directory separator of the host OS, which is backslashes for Windows and forward slashes for everything else.

Posted by Tyler Lesmann on January 15, 2009 at 5:41
Tagged as: python shell_scripting
Comments
#1 Simon wrote this 3 years ago

See http://code.activestate.com/recipes/499305/ for the helper function I usually use for this.

#2 Tyler Lesmann wrote this 3 years ago

That's pretty slick. I'll have to remember about fnmatch in the future. Thanks for the info. :)

Post a comment