16.01.2014 Views

Beginning Python - From Novice to Professional

Beginning Python - From Novice to Professional

Beginning Python - From Novice to Professional

SHOW MORE
SHOW LESS

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

218 CHAPTER 10 ■ BATTERIES INCLUDED<br />

Example<br />

Starting a Web browser. The system command can be used <strong>to</strong> execute any external program, which is very useful<br />

in environments such as UNIX where you can execute programs (or commands) from the command line <strong>to</strong> list the<br />

contents of a direc<strong>to</strong>ry, send e-mail, and so on. But it can be useful for starting programs with graphical user interfaces,<br />

<strong>to</strong>o—such as a Web browser. In UNIX, you can do the following (provided that you have a browser at /usr/<br />

bin/firefox):<br />

os.system('/usr/bin/firefox')<br />

A Windows version would be (again use the path of a browser you have installed)<br />

os.system(r'c:\"Program Files"\"Mozilla Firefox"\firefox.exe')<br />

Note that I’ve been careful about enclosing Program Files and Mozilla Firefox in quotes; otherwise DOS<br />

(which handles the command) balks at the whitespace. (This may be important for direc<strong>to</strong>ries in your PYTHONPATH<br />

as well.) Note also that you have <strong>to</strong> use backslashes here because DOS gets confused by forward slashes. If you run<br />

this, you will notice that the browser tries <strong>to</strong> open a Web site named Files"\Mozilla...—the part of the command<br />

after the whitespace. Also, if you try <strong>to</strong> run this from IDLE, a DOS window appears, but the browser doesn’t start until<br />

you close that DOS window. All in all, not exactly ideal behavior.<br />

Another function that suits the job better is the Windows-specific function os.startfile:<br />

os.startfile(r' c:\Program Files\Mozilla Firefox\firefox.exe')<br />

As you can see, os.startfile accepts a plain path, even if it contains whitespace. (That is, don’t enclose<br />

“Program Files” in quotes as in the os.system example.)<br />

Note that in Windows, your <strong>Python</strong> program keeps on running after the external program has been started by<br />

os.system (or os.startfile), whereas in UNIX, your <strong>Python</strong> program waits for the os.system command<br />

<strong>to</strong> finish.<br />

A BETTER SOLUTION: WEBBROWSER<br />

The os.system function is useful for a lot of things, but for the specific task of launching a Web browser<br />

there’s an even better solution: the webbrowser module. It contains a function called open that lets you au<strong>to</strong>matically<br />

launch a Web browser <strong>to</strong> open the given URL. For example, if you want your program <strong>to</strong> open the<br />

<strong>Python</strong> Web site in a Web browser (either starting a new browser or using one that is already running), you<br />

simply use<br />

import webbrowser<br />

webbrowser.open('http://www.python.org')<br />

and the page should pop up. Pretty nifty, huh?

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!