Python's principal advantage over languages such as Unix' Bourne Shell language or Windows' Command Prompt language is in its regularity as a programming language. When scripting with Python, you won't find yourself as often poring over the language documentation just to do something simple like looping, and many things a Bourne or Command Prompt programmer uses external programs for can be easily accomplished within Python. However, you have access to the power of such external programs, such as grep or sed, whenever you want them.
Example usage:
import commands : status, output = commands.getstatusoutput('grep "345$" %s' % userfile)
Function | Purpose |
---|---|
getstatusoutput(command) | Executes the command, and returns its completion status and output as a tuple. Equivalent to `command` in Bourne Shell. |
getoutput(command) | Executes the command, and returns its output. |
getstatus(file) | Executes the command ls -ld file and returns its output as a string. |
Example usage:
import os : os.system("rm -f %s" % dest_fn)
Function | Purpose |
---|---|
system(command) | Executes the command. No output or completion status is preserved. Equivalent (roughly) to command >/dev/null in Bourne Shell. |
The wonderful thing about Python as a scripting language is that you actually don't need to access external programs via the above commands very often. Much of the processing that could be done by programs such as awk, sed, tr, cut, head, tail, and even grep can be done effectively by the many available classes and functions in the Python libraries. Python programmers most often find themselves using external programs for truly complex processing such as compiling. Nonetheless, access to external programs and their completion codes is available when you want it.