Scripting Clinic: Dissecting a Live Python... Script - Page 2

By Carla Schroder | Posted May 26, 2004
Page 2 of 2   |  Back to Page 1
Print ArticleEmail Article
  • Share on Facebook
  • Share on Twitter
  • Share on LinkedIn
Continued From Page 1

if type == t[0] :

Python uses 0, not 1, as the starting point for arrays. So t[0] is the first element of the array t, web. And that's how Python knows the name we chose for that array.

type_exts = t
if type_exts == 0 :
return

If there is a match, type_exts will be an array. If it's 0, exit quietly. Otherwise, move on to listing matching file types.

for f in files :
if os.path.isdir(root + "/" + f) :

Some Python libraries have sub-packages which contain useful utilities. The os library, which we've already imported, includes a group of utilities called os.path which operate on filenames, including isdir, which checks whether its argument is a directory. We don't need to import os.path explicitly if we've already imported os.

find_in_dir(root + "/" + f, type)
continue

If f is a directory, not a plain file, then we'll call find_in_dir recursively, to continue searching the file tree. But we need to specify the relative path to the new directory; that's what (root + "/" + f, type) does:

root = the current path
"/" = a literal slash
f, type = new directory name

Let's say our root directory is /home. This expression adds up to /home/new directory name.

ext = string.lower(os.path.splitext(f)[1][1:])

Taking this line one step at a time:

os.path.splitext(f) splits the filename into a 2-item array
consisting of a filename (such as "pic007") and an extension
(such as ".JPG").

os.path.splitext(f)[1] is the second element of this array, e.g. ".JPG".

os.path.splitext(f)[1][1:] takes all but the first character of this string. This is a very useful feature of Python: the "slicing" operator, specified with square brackets and a colon. loaf[i:j] returns all of the elements of loaf from position i to position j; if i is left out, the beginning is implied, while leaving out j takes everything from i to the end. Slices can be used with either arrays or strings.

So if os.path.splitext(f)[1] is ".JPG", then os.path.splitext(f)[1][1:] is everything from the second character to the end, or "JPG" without the dot.

Finally, string.lower turns the string to lowercase, "jpg", so that we can compare it against our list of lower-case file extensions.

for e in type_exts[1:] :

This is the slice operator, used this time on an array, type_exts. The first element of the array is the type name ("image"), so we want to skip over it but loop over the rest of the array.

if e == ext :

A string comparison between our string ("jpg") and the current extension in the array.

print f
break

If it matches, then print it, then break out of this inner loop, in order to move on to the next filename.

This is the end of find_in_dir. Now we begin the main code, called when we run the script.

# main()
if len(sys.argv) < 3 :
print "Usage:", sys.argv[0], "type rootdir"
exit(1)

Make sure we have two arguments, and if not, print a usage statement.

sys.argv (from the library package "sys") is the array of arguments passed in. The first element (at index 0) is the program name. It's best to use argv[0] when printing error messages or warnings, rather than making assumptions that the program will always be called by the same name.

type = sys.argv[1]
root = sys.argv[2]

find_in_dir(root, type)

The routine find_in_dir does the heavy lifting. In a way, the # make it so section is like the movie star who lolls around the set while stand-ins and stunt doubles do all the hard stuff. Then the star strolls onstage for a closeup, then retires to let the stand-ins and stunt doubles toil some more.

Conclusion
This little script does a lot in a few lines, which is a nice characteristic of Python. Be sure to visit Python's home page for reference manuals and excellent tutorials.

Comment and Contribute
(Maximum characters: 1200). You have
characters left.
Get the Latest Scoop with Enterprise Networking Planet Newsletter