The sys module contains system-specific functionality. We have already seen that the sys.argv list contains the command line arguments. Let us see an example.
Example 14.1. Using sys.argv
#!/usr/bin/python # Filename : cat.py import sys ##### Functions ##### def readfile(filename): '''Print a file to the standard output.''' f = file(filename) while True: line = f.readline() if len(line) == 0: break print line, # The comma is to suppress additional newline. f.close() ##### Main ##### if len(sys.argv) < 2: print 'No action specified.' sys.exit() if sys.argv[1].startswith('--'): option = sys.argv[1][2:] # Fetch sys.argv[1] and copy the string except for first two characters if option == 'version': print 'Version 1.00' elif option == 'help': print '''\ This program prints files to the standard output. Any number of files can be specified. Options include: --version : Prints the version number and exits --help : Prints this help and exits''' else: print 'Unknown option.' sys.exit() else: for filename in sys.argv[1:]: readfile(filename)
$ python cmdline.py --nonsense Unknown option. $ python cmdline.py --help This program prints files to the standard output. Any number of files can be specified. Options include: --version : Prints the version number and exits --help : Prints this help and exits $ python cmdline.py --version Version 1.00 $ python cmdline.py poem.txt poemmore.txt Programming is fun When the work is done if (you wanna make your work also fun): use Python! Programming is fun When the work is done if (you wanna make your work more fun): use more Python!
This command works basically like the cat command familiar to Linux/BSD/Unix users. You just specify the names of some text files and this command will print them to the output for you.
When Python is not run in interactive mode (i.e. you are not using the interpreter prompt), there is always at least one item in the sys.argv list which is the name of the current Python program being run. This is accessed by sys.argv[0] since Python starts counting from 0. Similarly, if there is one command line argument to our program, then sys.argv will contain two items and sys.argv[1] refers to the second item.
To make the program more user-friendly, we have supplied certain options that the user can use to learn more about the program. We use the first argument to check if any options have been specified to our program. If the --version option is used, the version number of the command is printed. Similarly, when the --help option is used, some explanation about the program is printed. We make use of the sys.exit() function to exit the running program. You can optionally return a status code using this function. As always, see help(sys.exit) for details.
When only filenames are specified, the program then prints out the files, one by one, to the standard output. As an aside, the name cat is short for concatenate which is basically what this program does i.e. it can print out a file or attach (concatenate) two or more files together in the output.
The sys.version string gives you information about the version of Python that you have installed. The sys.version_info tuple gives an easier way of enabling Python-version specific components of your program.
$ python >>> import sys >>> sys.version '2.2.3 (#1, Oct 15 2003, 23:33:35) \n[GCC 3.3.1 20030930 (Red Hat Linux 3.3.1-6)]' >>> sys.version_info (2, 2, 3, 'final', 0) >>>
For experienced programmers, other items of interest in the sys module include sys.stdin, sys.stdout and sys.stderr which represent the standard input, standard output and standard error streams of your program respectively.
For all this and more, please see the Python Standard Documentation. Yes, see it right now.