Beginning Python - From Novice to Professional
Beginning Python - From Novice to Professional Beginning Python - From Novice to Professional
CHAPTER 10 ■ BATTERIES INCLUDED 217 Table 10-3. Some Important Functions and Variables in the os Module Function/Variable environ system(command) sep pathsep linesep urandom(n) Description Mapping with environment variables Executes an OS command in a subshell Separator used in paths Separator to separate paths Line separator ('\n', '\r', or '\r\n') Returns n bytes of cryptographically strong random data ■Tip A useful function for traversing directories is os.walk. Check it out in the Python Library Reference. The mapping os.environ contains environment variables described earlier in this chapter. For example, to access the environment variable PYTHONPATH, you would use the expression os.environ['PYTHONPATH']. This mapping can also be used to change environment variables, although not all platforms support this. The function os.system is used to run external programs. There are other functions for executing external programs, including execv, which exits the Python interpreter, yielding control to the executed program, and popen, which creates a file-like connection to the program. For more information about these functions, consult the standard library documentation. ■Tip The subprocess module is a recent addition to Python, which collects the functionality of the os.system, execv, and popen functions. The module variable os.sep is a separator used in pathnames. The standard separator in UNIX is '/', the standard in Windows is '\\' (the Python syntax for a single backslash), and in Mac OS, it is ':'. (On some platforms, os.altsep contains an alternate path separator, such as '/' in Windows.) You use os.pathsep when grouping several paths, as in PYTHONPATH. The pathsep is used to separate the pathnames: ':' in UNIX, ';' in Windows, and '::' in Mac OS. The module variable os.linesep is the line separator string used in text files. In UNIX this is a single newline character ('\n'), in Mac OS it’s a single carriage return character ('\r'), and in Windows it’s the combination of a carriage return and a newline ('\r\n'). The urandom function uses a system-dependent source of “real” (or, at least, cryptographically strong) randomness. If your platform doesn’t support it, you’ll get a NotImplementedError.
218 CHAPTER 10 ■ BATTERIES INCLUDED Example Starting a Web browser. The system command can be used to execute any external program, which is very useful in environments such as UNIX where you can execute programs (or commands) from the command line to list the contents of a directory, send e-mail, and so on. But it can be useful for starting programs with graphical user interfaces, too—such as a Web browser. In UNIX, you can do the following (provided that you have a browser at /usr/ bin/firefox): os.system('/usr/bin/firefox') A Windows version would be (again use the path of a browser you have installed) os.system(r'c:\"Program Files"\"Mozilla Firefox"\firefox.exe') Note that I’ve been careful about enclosing Program Files and Mozilla Firefox in quotes; otherwise DOS (which handles the command) balks at the whitespace. (This may be important for directories in your PYTHONPATH as well.) Note also that you have to use backslashes here because DOS gets confused by forward slashes. If you run this, you will notice that the browser tries to open a Web site named Files"\Mozilla...—the part of the command after the whitespace. Also, if you try to run this from IDLE, a DOS window appears, but the browser doesn’t start until you close that DOS window. All in all, not exactly ideal behavior. Another function that suits the job better is the Windows-specific function os.startfile: os.startfile(r' c:\Program Files\Mozilla Firefox\firefox.exe') As you can see, os.startfile accepts a plain path, even if it contains whitespace. (That is, don’t enclose “Program Files” in quotes as in the os.system example.) Note that in Windows, your Python program keeps on running after the external program has been started by os.system (or os.startfile), whereas in UNIX, your Python program waits for the os.system command to finish. A BETTER SOLUTION: WEBBROWSER The os.system function is useful for a lot of things, but for the specific task of launching a Web browser there’s an even better solution: the webbrowser module. It contains a function called open that lets you automatically launch a Web browser to open the given URL. For example, if you want your program to open the Python Web site in a Web browser (either starting a new browser or using one that is already running), you simply use import webbrowser webbrowser.open('http://www.python.org') and the page should pop up. Pretty nifty, huh?
- 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 246 and 247: 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
- Page 297 and 298: 266 CHAPTER 11 ■ FILES AND STUFF
CHAPTER 10 ■ BATTERIES INCLUDED 217<br />
Table 10-3. Some Important Functions and Variables in the os Module<br />
Function/Variable<br />
environ<br />
system(command)<br />
sep<br />
pathsep<br />
linesep<br />
urandom(n)<br />
Description<br />
Mapping with environment variables<br />
Executes an OS command in a subshell<br />
Separa<strong>to</strong>r used in paths<br />
Separa<strong>to</strong>r <strong>to</strong> separate paths<br />
Line separa<strong>to</strong>r ('\n', '\r', or '\r\n')<br />
Returns n bytes of cryp<strong>to</strong>graphically strong random data<br />
■Tip A useful function for traversing direc<strong>to</strong>ries is os.walk. Check it out in the <strong>Python</strong> Library Reference.<br />
The mapping os.environ contains environment variables described earlier in this chapter.<br />
For example, <strong>to</strong> access the environment variable PYTHONPATH, you would use the expression<br />
os.environ['PYTHONPATH']. This mapping can also be used <strong>to</strong> change environment variables,<br />
although not all platforms support this.<br />
The function os.system is used <strong>to</strong> run external programs. There are other functions for<br />
executing external programs, including execv, which exits the <strong>Python</strong> interpreter, yielding<br />
control <strong>to</strong> the executed program, and popen, which creates a file-like connection <strong>to</strong> the program.<br />
For more information about these functions, consult the standard library documentation.<br />
■Tip The subprocess module is a recent addition <strong>to</strong> <strong>Python</strong>, which collects the functionality of the<br />
os.system, execv, and popen functions.<br />
The module variable os.sep is a separa<strong>to</strong>r used in pathnames. The standard separa<strong>to</strong>r in<br />
UNIX is '/', the standard in Windows is '\\' (the <strong>Python</strong> syntax for a single backslash), and in<br />
Mac OS, it is ':'. (On some platforms, os.altsep contains an alternate path separa<strong>to</strong>r, such as<br />
'/' in Windows.)<br />
You use os.pathsep when grouping several paths, as in PYTHONPATH. The pathsep is used <strong>to</strong><br />
separate the pathnames: ':' in UNIX, ';' in Windows, and '::' in Mac OS.<br />
The module variable os.linesep is the line separa<strong>to</strong>r string used in text files. In UNIX this<br />
is a single newline character ('\n'), in Mac OS it’s a single carriage return character ('\r'), and<br />
in Windows it’s the combination of a carriage return and a newline ('\r\n').<br />
The urandom function uses a system-dependent source of “real” (or, at least, cryp<strong>to</strong>graphically<br />
strong) randomness. If your platform doesn’t support it, you’ll get a NotImplementedError.