Beginning Python - From Novice to Professional
Beginning Python - From Novice to Professional Beginning Python - From Novice to Professional
CHAPTER 10 ■ BATTERIES INCLUDED 215 The Standard Library: A Few Favorites Chances are that you’re beginning to wonder what the title of this chapter means. The phrase was originally coined by Frank Stajano and refers to Python’s copious standard library. When you install Python, you get lots of useful modules (the batteries) for “free.” Because there are so many ways of getting more information about these modules (as explained in the first part of this chapter), I won’t include a full reference here (which would take up far too much space anyway), but I’ll describe a few of my favorite standard modules to whet your appetite for exploration. You’ll encounter more standard modules in the project chapters (Chapter 20 and later). The module descriptions are not complete but highlight some of the interesting features of each module. sys This module gives you access to variables and functions that are closely linked to the Python interpreter. Some of these are shown in Table 10-2. Table 10-2. Some Important Functions and Variables in the sys Module Function/Variable argv exit([arg]) modules path platform stdin stdout stderr Description The command-line arguments, including the script name Exits the current program, optionally with a given return value or error message A dictionary mapping module names to loaded modules A list of directory names where modules can be found Contains a platform identifier such as sunos5 or win32 Standard input stream—a file-like object Standard output stream—a file-like object Standard error stream—a file-like object The variable sys.argv contains the arguments passed to the Python interpreter, including the script name. The function sys.exit exits the current program. (If called within a try/finally block, the finally clause is executed.) You can supply an integer to indicate whether the program succeeded or not—a UNIX convention. You’ll probably be fine in most cases if you rely on the default (which is zero, indicating success). Alternatively, you can supply a string, which is used as an error message and can be very useful for a user trying to figure out why the program halted; then, the program exits with that error message and a code indicating failure. The mapping sys.modules maps module names to actual modules. It only applies to currently imported modules. The module variable sys.path was discussed earlier in this chapter. It’s a list of strings, in which each string is the name of a directory where the interpreter will look for modules when an import statement is executed.
216 CHAPTER 10 ■ BATTERIES INCLUDED The module variable sys.platform (a string) is simply the name of the “platform” the interpreter is running on. This may be either a name indicating an operating system (such as sunos5 or win32) or it may indicate some other kind of platform, such as a Java virtual machine (for example, java1.4.0) if you’re running Jython. The module variables sys.stdin, sys.stdout, and sys.stderr are file-like stream objects. They represent the standard UNIX concepts of standard input, standard output, and standard error. To put it simply, sys.stdin is where Python gets its input (used in the functions input and raw_input, for example), and sys.stdout is where it prints to. You learn more about files (and these three streams) in Chapter 11. Example Printing the arguments in reverse order. When you call a Python script from the command line, you may add some arguments after it—the so-called command-line arguments. These will then be placed in the list sys.argv, with the name of the Python script as sys.argv[0]. Printing these out in reverse order is pretty simple, as you can see in Listing 10-5. Listing 10-5. Reversing and Printing Command-Line Arguments # reverseargs.py import sys args = sys.argv[1:] args.reverse() print ' '.join(args) As you can see, I make a copy of sys.argv. You can modify the original, but in general it’s safer not to because other parts of the program may also rely on sys.argv containing the original arguments. Notice also that I skip the first element of sys.argv—the name of the script. I reverse the list with args.reverse(), but I can’t print the result of that operation. It is an in-place modification that returns None. Finally, to make the output prettier, I use the join string method. Let’s try the result (assuming a UNIX shell here, but it will work equally well at an MS-DOS prompt, for example): $ python reverseargs.py this is a test test a is this os The os module gives you access to several operating system services. The os module is extensive, and only a few of the most useful functions and variables are described in Table 10-3. In addition to these, os and its submodule os.path contain several functions to examine, construct, and remove directories and files. For more information about this functionality, see the standard library documentation.
- Page 195 and 196: 164 CHAPTER 8 ■ EXCEPTIONS More T
- Page 197 and 198: 166 CHAPTER 8 ■ EXCEPTIONS try: x
- Page 199 and 200: 168 CHAPTER 8 ■ EXCEPTIONS Invali
- Page 201 and 202: 170 CHAPTER 8 ■ EXCEPTIONS If you
- Page 204 and 205: CHAPTER 9 ■ ■ ■ Magic Methods
- Page 206 and 207: CHAPTER 9 ■ MAGIC METHODS, PROPER
- Page 208 and 209: CHAPTER 9 ■ MAGIC METHODS, PROPER
- Page 210 and 211: CHAPTER 9 ■ MAGIC METHODS, PROPER
- Page 212 and 213: CHAPTER 9 ■ MAGIC METHODS, PROPER
- Page 214 and 215: CHAPTER 9 ■ MAGIC METHODS, PROPER
- Page 216 and 217: CHAPTER 9 ■ MAGIC METHODS, PROPER
- Page 218 and 219: CHAPTER 9 ■ MAGIC METHODS, PROPER
- Page 220 and 221: CHAPTER 9 ■ MAGIC METHODS, PROPER
- Page 222 and 223: CHAPTER 9 ■ MAGIC METHODS, PROPER
- Page 224 and 225: CHAPTER 9 ■ MAGIC METHODS, PROPER
- Page 226 and 227: CHAPTER 9 ■ MAGIC METHODS, PROPER
- Page 228 and 229: CHAPTER 9 ■ MAGIC METHODS, PROPER
- Page 230 and 231: CHAPTER 9 ■ MAGIC METHODS, PROPER
- Page 232 and 233: CHAPTER 9 ■ MAGIC METHODS, PROPER
- Page 234 and 235: CHAPTER 10 ■ ■ ■ Batteries In
- Page 236 and 237: CHAPTER 10 ■ BATTERIES INCLUDED 2
- Page 238 and 239: CHAPTER 10 ■ BATTERIES INCLUDED 2
- Page 240 and 241: CHAPTER 10 ■ BATTERIES INCLUDED 2
- Page 242 and 243: CHAPTER 10 ■ BATTERIES INCLUDED 2
- Page 244 and 245: CHAPTER 10 ■ BATTERIES INCLUDED 2
- Page 248 and 249: CHAPTER 10 ■ BATTERIES INCLUDED 2
- Page 250 and 251: CHAPTER 10 ■ BATTERIES INCLUDED 2
- Page 252 and 253: CHAPTER 10 ■ BATTERIES INCLUDED 2
- Page 254 and 255: CHAPTER 10 ■ BATTERIES INCLUDED 2
- Page 256 and 257: CHAPTER 10 ■ BATTERIES INCLUDED 2
- Page 258 and 259: CHAPTER 10 ■ BATTERIES INCLUDED 2
- Page 260 and 261: CHAPTER 10 ■ BATTERIES INCLUDED 2
- Page 262 and 263: CHAPTER 10 ■ BATTERIES INCLUDED 2
- Page 264 and 265: CHAPTER 10 ■ BATTERIES INCLUDED 2
- Page 266 and 267: CHAPTER 10 ■ BATTERIES INCLUDED 2
- Page 268 and 269: CHAPTER 10 ■ BATTERIES INCLUDED 2
- Page 270 and 271: CHAPTER 10 ■ BATTERIES INCLUDED 2
- Page 272 and 273: CHAPTER 10 ■ BATTERIES INCLUDED 2
- Page 274 and 275: CHAPTER 10 ■ BATTERIES INCLUDED 2
- Page 276 and 277: CHAPTER 10 ■ BATTERIES INCLUDED 2
- Page 278 and 279: CHAPTER 10 ■ BATTERIES INCLUDED 2
- Page 280 and 281: CHAPTER 10 ■ BATTERIES INCLUDED 2
- Page 282 and 283: CHAPTER 10 ■ BATTERIES INCLUDED 2
- Page 284: CHAPTER 10 ■ BATTERIES INCLUDED 2
- Page 287 and 288: 256 CHAPTER 11 ■ FILES AND STUFF
- Page 289 and 290: 258 CHAPTER 11 ■ FILES AND STUFF
- Page 291 and 292: 260 CHAPTER 11 ■ FILES AND STUFF
- Page 293 and 294: 262 CHAPTER 11 ■ FILES AND STUFF
- Page 295 and 296: 264 CHAPTER 11 ■ FILES AND STUFF
216 CHAPTER 10 ■ BATTERIES INCLUDED<br />
The module variable sys.platform (a string) is simply the name of the “platform” the<br />
interpreter is running on. This may be either a name indicating an operating system (such as<br />
sunos5 or win32) or it may indicate some other kind of platform, such as a Java virtual machine<br />
(for example, java1.4.0) if you’re running Jython.<br />
The module variables sys.stdin, sys.stdout, and sys.stderr are file-like stream objects.<br />
They represent the standard UNIX concepts of standard input, standard output, and standard<br />
error. To put it simply, sys.stdin is where <strong>Python</strong> gets its input (used in the functions input<br />
and raw_input, for example), and sys.stdout is where it prints <strong>to</strong>. You learn more about files<br />
(and these three streams) in Chapter 11.<br />
Example<br />
Printing the arguments in reverse order. When you call a <strong>Python</strong> script from the command line, you may add<br />
some arguments after it—the so-called command-line arguments. These will then be placed in the list sys.argv,<br />
with the name of the <strong>Python</strong> script as sys.argv[0]. Printing these out in reverse order is pretty simple, as you can<br />
see in Listing 10-5.<br />
Listing 10-5. Reversing and Printing Command-Line Arguments<br />
# reverseargs.py<br />
import sys<br />
args = sys.argv[1:]<br />
args.reverse()<br />
print ' '.join(args)<br />
As you can see, I make a copy of sys.argv. You can modify the original, but in general it’s safer not <strong>to</strong> because<br />
other parts of the program may also rely on sys.argv containing the original arguments. Notice also that I skip the<br />
first element of sys.argv—the name of the script. I reverse the list with args.reverse(), but I can’t print the<br />
result of that operation. It is an in-place modification that returns None. Finally, <strong>to</strong> make the output prettier, I use the<br />
join string method. Let’s try the result (assuming a UNIX shell here, but it will work equally well at an MS-DOS<br />
prompt, for example):<br />
$ python reverseargs.py this is a test<br />
test a is this<br />
os<br />
The os module gives you access <strong>to</strong> several operating system services. The os module is extensive,<br />
and only a few of the most useful functions and variables are described in Table 10-3. In addition <strong>to</strong><br />
these, os and its submodule os.path contain several functions <strong>to</strong> examine, construct, and remove<br />
direc<strong>to</strong>ries and files. For more information about this functionality, see the standard library<br />
documentation.