|
|
| (2 intermediate revisions by 2 users not shown) |
| Line 1: |
Line 1: |
| <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN"
| | {{Think Python/Page}} |
| "http://www.w3.org/TR/REC-html40/loose.dtd">
| |
| <HTML>
| |
| <HEAD>
| |
|
| |
|
| <META http-equiv="Content-Type" content="text/html; charset=US-ASCII">
| | == Chapter 14  Files == |
| <META name="GENERATOR" content="hevea 1.10">
| | |
| <LINK rel="stylesheet" type="text/css" href="book.css">
| | |
| <TITLE>Files</TITLE>
| | |
| </HEAD>
| | === 14.1  Persistence === |
| <BODY >
| | |
| <A HREF="book014.html"><IMG SRC="previous_motif.gif" ALT="Previous"></A>
| | Most of the programs we have seen so far are transient in the |
| <A HREF="index.html"><IMG SRC="contents_motif.gif" ALT="Up"></A>
| |
| <A HREF="book016.html"><IMG SRC="next_motif.gif" ALT="Next"></A>
| |
| <HR>
| |
| <H1 CLASS="chapter"><A NAME="htoc168"><FONT COLOR=black><FONT SIZE=3>Chapter 14</FONT></FONT></A><FONT COLOR=black><FONT SIZE=3>  Files</FONT></FONT></H1><P><A NAME="@default1229"></A><FONT COLOR=black><FONT SIZE=3>
| |
| </FONT></FONT><A NAME="@default1230"></A></P><H2 CLASS="section"><A NAME="toc153"></A><A NAME="htoc169"><FONT COLOR=black><FONT SIZE=3>14.1</FONT></FONT></A><FONT COLOR=black><FONT SIZE=3>  Persistence</FONT></FONT></H2><P><A NAME="@default1231"></A></P><P><FONT COLOR=black><FONT SIZE=3>Most of the programs we have seen so far are transient in the
| |
| sense that they run for a short time and produce some output, | | sense that they run for a short time and produce some output, |
| but when they end, their data disappears. If you run the program | | but when they end, their data disappears. If you run the program |
| again, it starts with a clean slate.</FONT></FONT></P><P><FONT COLOR=black><FONT SIZE=3>Other programs are </FONT></FONT><FONT COLOR=black><FONT SIZE=3><B>persistent</B></FONT></FONT><FONT COLOR=black><FONT SIZE=3>: they run for a long time | | again, it starts with a clean slate. |
| | |
| | Other programs are '''persistent''': they run for a long time |
| (or all the time); they keep at least some of their data | | (or all the time); they keep at least some of their data |
| in permanent storage (a hard drive, for example); and | | in permanent storage (a hard drive, for example); and |
| if they shut down and restart, they pick up where they left off.</FONT></FONT></P><P><FONT COLOR=black><FONT SIZE=3>Examples of persistent programs are operating systems, which | | if they shut down and restart, they pick up where they left off. |
| | |
| | Examples of persistent programs are operating systems, which |
| run pretty much whenever a computer is on, and web servers, | | run pretty much whenever a computer is on, and web servers, |
| which run all the time, waiting for requests to come in on | | which run all the time, waiting for requests to come in on |
| the network.</FONT></FONT></P><P><FONT COLOR=black><FONT SIZE=3>One of the simplest ways for programs to maintain their data | | the network. |
| | |
| | One of the simplest ways for programs to maintain their data |
| is by reading and writing text files. We have already seen | | is by reading and writing text files. We have already seen |
| programs that read text files; in this chapters we will see programs | | programs that read text files; in this chapters we will see programs |
| that write them.</FONT></FONT></P><P><FONT COLOR=black><FONT SIZE=3>An alternative is to store the state of the program in a database. | | that write them. |
| | |
| | An alternative is to store the state of the program in a database. |
| In this chapter I will present a simple database and a module, | | In this chapter I will present a simple database and a module, |
| </FONT></FONT><FONT COLOR=black><FONT SIZE=3><TT>pickle</TT></FONT></FONT><FONT COLOR=black><FONT SIZE=3>, that makes it easy to store program data.</FONT></FONT></P><P><A NAME="@default1232"></A><FONT COLOR=black><FONT SIZE=3>
| | <TT>pickle</TT>, that makes it easy to store program data. |
| </FONT></FONT><A NAME="@default1233"></A></P><H2 CLASS="section"><A NAME="toc154"></A><A NAME="htoc170"><FONT COLOR=black><FONT SIZE=3>14.2</FONT></FONT></A><FONT COLOR=black><FONT SIZE=3>  Reading and writing</FONT></FONT></H2><P><A NAME="@default1234"></A></P><P><FONT COLOR=black><FONT SIZE=3>A text file is a sequence of characters stored on a permanent
| | |
| | |
| | |
| | === 14.2  Reading and writing === |
| | |
| | A text file is a sequence of characters stored on a permanent |
| medium like a hard drive, flash memory, or CD-ROM. We saw how | | medium like a hard drive, flash memory, or CD-ROM. We saw how |
| to open and read a file in Section </FONT></FONT><A HREF="book010.html#wordlist"><FONT COLOR=black><FONT SIZE=3>9.1</FONT></FONT></A><FONT COLOR=black><FONT SIZE=3>.</FONT></FONT></P><P><A NAME="@default1235"></A><FONT COLOR=black><FONT SIZE=3> | | to open and read a file in Section 9.1. |
| </FONT></FONT><A NAME="@default1236"></A></P><P><FONT COLOR=black><FONT SIZE=3>To write a file, you have to open it with mode
| | |
| </FONT></FONT><CODE><FONT COLOR=black><FONT SIZE=3>'w'</FONT></FONT></CODE><FONT COLOR=black><FONT SIZE=3> as a second parameter:</FONT></FONT></P><PRE CLASS="verbatim"><FONT COLOR=blue><FONT SIZE=4>>>> fout = open('output.txt', 'w')
| | |
| | |
| | |
| | To write a file, you have to open it with mode |
| | <CODE>'w'</CODE> as a second parameter: |
| | <PRE CLASS="verbatim">>>> fout = open('output.txt', 'w') |
| >>> print fout | | >>> print fout |
| <open file 'output.txt', mode 'w' at 0xb7eb2410> | | <open file 'output.txt', mode 'w' at 0xb7eb2410> |
| </FONT></FONT></PRE><P><FONT COLOR=black><FONT SIZE=3>If the file already exists, opening it in write mode clears out
| | </PRE> |
| | If the file already exists, opening it in write mode clears out |
| the old data and starts fresh, so be careful! | | the old data and starts fresh, so be careful! |
| If the file doesn’t exist, a new one is created.</FONT></FONT></P><P><FONT COLOR=black><FONT SIZE=3>The </FONT></FONT><FONT COLOR=black><FONT SIZE=3><TT>write</TT></FONT></FONT><FONT COLOR=black><FONT SIZE=3> method puts data into the file.</FONT></FONT></P><PRE CLASS="verbatim"><FONT COLOR=blue><FONT SIZE=4>>>> line1 = "This here's the wattle,\n" | | If the file doesn’t exist, a new one is created. |
| | |
| | The <TT>write</TT> method puts data into the file. |
| | <PRE CLASS="verbatim">>>> line1 = "This here's the wattle,\n" |
| >>> fout.write(line1) | | >>> fout.write(line1) |
| </FONT></FONT></PRE><P><FONT COLOR=black><FONT SIZE=3>Again, the file object keeps track of where it is, so if
| | </PRE> |
| you call </FONT></FONT><FONT COLOR=black><FONT SIZE=3><TT>write</TT></FONT></FONT><FONT COLOR=black><FONT SIZE=3> again, it adds the new data to the end.</FONT></FONT></P><PRE CLASS="verbatim"><FONT COLOR=blue><FONT SIZE=4>>>> line2 = "the emblem of our land.\n" | | Again, the file object keeps track of where it is, so if |
| | you call <TT>write</TT> again, it adds the new data to the end. |
| | <PRE CLASS="verbatim">>>> line2 = "the emblem of our land.\n" |
| >>> fout.write(line2) | | >>> fout.write(line2) |
| </FONT></FONT></PRE><P><FONT COLOR=black><FONT SIZE=3>When you are done writing, you have to close the file.</FONT></FONT></P><PRE CLASS="verbatim"><FONT COLOR=blue><FONT SIZE=4>>>> fout.close()
| | </PRE> |
| </FONT></FONT></PRE><P><A NAME="@default1237"></A><FONT COLOR=black><FONT SIZE=3>
| | When you are done writing, you have to close the file. |
| </FONT></FONT><A NAME="@default1238"></A></P><H2 CLASS="section"><A NAME="toc155"></A><A NAME="htoc171"><FONT COLOR=black><FONT SIZE=3>14.3</FONT></FONT></A><FONT COLOR=black><FONT SIZE=3>  Format operator</FONT></FONT></H2><P><A NAME="@default1239"></A><FONT COLOR=black><FONT SIZE=3>
| | <PRE CLASS="verbatim">>>> fout.close() |
| </FONT></FONT><A NAME="@default1240"></A></P><P><FONT COLOR=black><FONT SIZE=3>The argument of </FONT></FONT><FONT COLOR=black><FONT SIZE=3><TT>write</TT></FONT></FONT><FONT COLOR=black><FONT SIZE=3> has to be a string, so if we want
| | </PRE> |
| | |
| | |
| | === 14.3  Format operator === |
| | |
| | |
| | |
| | |
| | The argument of <TT>write</TT> has to be a string, so if we want |
| to put other values in a file, we have to convert them to | | to put other values in a file, we have to convert them to |
| strings. The easiest way to do that is with </FONT></FONT><FONT COLOR=black><FONT SIZE=3><TT>str</TT></FONT></FONT><FONT COLOR=black><FONT SIZE=3>:</FONT></FONT></P><PRE CLASS="verbatim"><FONT COLOR=blue><FONT SIZE=4>>>> x = 52 | | strings. The easiest way to do that is with <TT>str</TT>: |
| | <PRE CLASS="verbatim">>>> x = 52 |
| >>> f.write(str(x)) | | >>> f.write(str(x)) |
| </FONT></FONT></PRE><P><FONT COLOR=black><FONT SIZE=3>An alternative is to use the </FONT></FONT><FONT COLOR=black><FONT SIZE=3><B>format operator</B></FONT></FONT><FONT COLOR=black><FONT SIZE=3>, </FONT></FONT><FONT COLOR=black><FONT SIZE=3><TT>%</TT></FONT></FONT><FONT COLOR=black><FONT SIZE=3>. When
| | </PRE> |
| applied to integers, </FONT></FONT><FONT COLOR=black><FONT SIZE=3><TT>%</TT></FONT></FONT><FONT COLOR=black><FONT SIZE=3> is the modulus operator. But | | An alternative is to use the '''format operator''', <TT>%</TT>. When |
| when the first operand is a string, </FONT></FONT><FONT COLOR=black><FONT SIZE=3><TT>%</TT></FONT></FONT><FONT COLOR=black><FONT SIZE=3> is the format operator.</FONT></FONT></P><P><A NAME="@default1241"></A></P><P><FONT COLOR=black><FONT SIZE=3>The first operand is the </FONT></FONT><FONT COLOR=black><FONT SIZE=3><B>format string</B></FONT></FONT><FONT COLOR=black><FONT SIZE=3>, and the second operand | | applied to integers, <TT>%</TT> is the modulus operator. But |
| | when the first operand is a string, <TT>%</TT> is the format operator. |
| | |
| | The first operand is the '''format string''', and the second operand |
| is a tuple of expressions. The result is a string that contains | | is a tuple of expressions. The result is a string that contains |
| the values of the expressions, formatted according to the format | | the values of the expressions, formatted according to the format |
| string.</FONT></FONT></P><P><A NAME="@default1242"></A></P><P><FONT COLOR=black><FONT SIZE=3>As an example, the </FONT></FONT><FONT COLOR=black><FONT SIZE=3><B>format sequence</B></FONT></FONT><FONT COLOR=black><FONT SIZE=3> </FONT></FONT><CODE><FONT COLOR=black><FONT SIZE=3>'%d'</FONT></FONT></CODE><FONT COLOR=black><FONT SIZE=3> means that | | string. |
| | |
| | As an example, the '''format sequence''' <CODE>'%d'</CODE> means that |
| the first expression in the tuple should be formatted as an | | the first expression in the tuple should be formatted as an |
| integer (</FONT></FONT><FONT COLOR=black><FONT SIZE=3><TT>d</TT></FONT></FONT><FONT COLOR=black><FONT SIZE=3> stands for “decimal”):</FONT></FONT></P><PRE CLASS="verbatim"><FONT COLOR=blue><FONT SIZE=4>>>> camels = 42 | | integer (<TT>d</TT> stands for “decimal”): |
| | <PRE CLASS="verbatim">>>> camels = 42 |
| >>> '%d' % camels | | >>> '%d' % camels |
| '42' | | '42' |
| </FONT></FONT></PRE><P><FONT COLOR=black><FONT SIZE=3>The result is the string </FONT></FONT><CODE><FONT COLOR=black><FONT SIZE=3>'42'</FONT></FONT></CODE><FONT COLOR=black><FONT SIZE=3>, which is not to be confused
| | </PRE> |
| with the integer value </FONT></FONT><FONT COLOR=black><FONT SIZE=3><TT>42</TT></FONT></FONT><FONT COLOR=black><FONT SIZE=3>.</FONT></FONT></P><P><FONT COLOR=black><FONT SIZE=3>A format sequence can appear anywhere in the format string, | | The result is the string <CODE>'42'</CODE>, which is not to be confused |
| so you can embed a value in a sentence:</FONT></FONT></P><PRE CLASS="verbatim"><FONT COLOR=blue><FONT SIZE=4>>>> camels = 42 | | with the integer value <TT>42</TT>. |
| | |
| | A format sequence can appear anywhere in the format string, |
| | so you can embed a value in a sentence: |
| | <PRE CLASS="verbatim">>>> camels = 42 |
| >>> 'I have spotted %d camels.' % camels | | >>> 'I have spotted %d camels.' % camels |
| 'I have spotted 42 camels.' | | 'I have spotted 42 camels.' |
| </FONT></FONT></PRE><P><FONT COLOR=black><FONT SIZE=3>The format sequence </FONT></FONT><CODE><FONT COLOR=black><FONT SIZE=3>'%g'</FONT></FONT></CODE><FONT COLOR=black><FONT SIZE=3> formats the next element in the tuple
| | </PRE> |
| as a floating-point number (don’t ask why), and </FONT></FONT><CODE><FONT COLOR=black><FONT SIZE=3>'%s'</FONT></FONT></CODE><FONT COLOR=black><FONT SIZE=3> formats | | The format sequence <CODE>'%g'</CODE> formats the next element in the tuple |
| the next item as a string:</FONT></FONT></P><PRE CLASS="verbatim"><FONT COLOR=blue><FONT SIZE=4>>>> 'In %d years I have spotted %g %s.' % (3, 0.1, 'camels') | | as a floating-point number (don’t ask why), and <CODE>'%s'</CODE> formats |
| | the next item as a string: |
| | <PRE CLASS="verbatim">>>> 'In %d years I have spotted %g %s.' % (3, 0.1, 'camels') |
| 'In 3 years I have spotted 0.1 camels.' | | 'In 3 years I have spotted 0.1 camels.' |
| </FONT></FONT></PRE><P><FONT COLOR=black><FONT SIZE=3>The number of elements in the tuple has to match the number
| | </PRE> |
| | The number of elements in the tuple has to match the number |
| of format sequences in the string. Also, the types of the | | of format sequences in the string. Also, the types of the |
| elements have to match the format sequences:</FONT></FONT></P><P><A NAME="@default1243"></A><FONT COLOR=black><FONT SIZE=3> | | elements have to match the format sequences: |
| </FONT></FONT><A NAME="@default1244"></A></P><PRE CLASS="verbatim"><FONT COLOR=blue><FONT SIZE=4>>>> '%d %d %d' % (1, 2)
| | |
| | |
| | |
| | <PRE CLASS="verbatim">>>> '%d %d %d' % (1, 2) |
| TypeError: not enough arguments for format string | | TypeError: not enough arguments for format string |
| >>> '%d' % 'dollars' | | >>> '%d' % 'dollars' |
| TypeError: illegal argument type for built-in operation | | TypeError: illegal argument type for built-in operation |
| </FONT></FONT></PRE><P><FONT COLOR=black><FONT SIZE=3>In the first example, there aren’t enough elements; in the
| | </PRE> |
| second, the element is the wrong type.</FONT></FONT></P><P><FONT COLOR=black><FONT SIZE=3>The format operator is powerful but difficult to use. You can | | In the first example, there aren’t enough elements; in the |
| read more about it at </FONT></FONT><FONT COLOR=black><FONT SIZE=3><TT>docs.python.org/lib/typesseq-strings.html</TT></FONT></FONT><FONT COLOR=black><FONT SIZE=3>.</FONT></FONT></P><H2 CLASS="section"><A NAME="toc156"></A><A NAME="htoc172"><FONT COLOR=black><FONT SIZE=3>14.4</FONT></FONT></A><FONT COLOR=black><FONT SIZE=3>  Filenames and paths</FONT></FONT></H2><P><FONT COLOR=black><FONT SIZE=3> | | second, the element is the wrong type. |
| </FONT></FONT><A NAME="paths"></A></P><P><A NAME="@default1245"></A><FONT COLOR=black><FONT SIZE=3>
| | |
| </FONT></FONT><A NAME="@default1246"></A><FONT COLOR=black><FONT SIZE=3>
| | The format operator is powerful but difficult to use. You can |
| </FONT></FONT><A NAME="@default1247"></A><FONT COLOR=black><FONT SIZE=3>
| | read more about it at <TT>docs.python.org/lib/typesseq-strings.html</TT>. |
| </FONT></FONT><A NAME="@default1248"></A></P><P><FONT COLOR=black><FONT SIZE=3>Files are organized into </FONT></FONT><FONT COLOR=black><FONT SIZE=3><B>directories</B></FONT></FONT><FONT COLOR=black><FONT SIZE=3> (also called “folders”).
| | === 14.4  Filenames and paths === |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | Files are organized into '''directories''' (also called “folders”). |
| Every running program has a “current directory,” which is the | | Every running program has a “current directory,” which is the |
| default directory for most operations. | | default directory for most operations. |
| For example, when you open a file for reading, Python looks for it in the | | For example, when you open a file for reading, Python looks for it in the |
| current directory.</FONT></FONT></P><P><A NAME="@default1249"></A><FONT COLOR=black><FONT SIZE=3> | | current directory. |
| </FONT></FONT><A NAME="@default1250"></A></P><P><FONT COLOR=black><FONT SIZE=3>The </FONT></FONT><FONT COLOR=black><FONT SIZE=3><TT>os</TT></FONT></FONT><FONT COLOR=black><FONT SIZE=3> module provides functions for working with files and
| | |
| directories (“os” stands for “operating system”). </FONT></FONT><FONT COLOR=black><FONT SIZE=3><TT>os.getcwd</TT></FONT></FONT><FONT COLOR=black><FONT SIZE=3> | | |
| returns the name of the current directory:</FONT></FONT></P><P><A NAME="@default1251"></A><FONT COLOR=black><FONT SIZE=3> | | |
| </FONT></FONT><A NAME="@default1252"></A></P><PRE CLASS="verbatim"><FONT COLOR=blue><FONT SIZE=4>>>> import os
| | |
| | The <TT>os</TT> module provides functions for working with files and |
| | directories (“os” stands for “operating system”). <TT>os.getcwd</TT> |
| | returns the name of the current directory: |
| | |
| | |
| | |
| | <PRE CLASS="verbatim">>>> import os |
| >>> cwd = os.getcwd() | | >>> cwd = os.getcwd() |
| >>> print cwd | | >>> print cwd |
| /home/dinsdale | | /home/dinsdale |
| </FONT></FONT></PRE><P><FONT COLOR=black><FONT SIZE=3><TT>cwd</TT></FONT></FONT><FONT COLOR=black><FONT SIZE=3> stands for “current working directory.” The result in
| | </PRE> |
| this example is </FONT></FONT><FONT COLOR=black><FONT SIZE=3><TT>/home/dinsdale</TT></FONT></FONT><FONT COLOR=black><FONT SIZE=3>, which is the home directory of a | | <TT>cwd</TT> stands for “current working directory.” The result in |
| user named </FONT></FONT><FONT COLOR=black><FONT SIZE=3><TT>dinsdale</TT></FONT></FONT><FONT COLOR=black><FONT SIZE=3>.</FONT></FONT></P><P><A NAME="@default1253"></A><FONT COLOR=black><FONT SIZE=3> | | this example is <TT>/home/dinsdale</TT>, which is the home directory of a |
| </FONT></FONT><A NAME="@default1254"></A></P><P><FONT COLOR=black><FONT SIZE=3>A string like </FONT></FONT><FONT COLOR=black><FONT SIZE=3><TT>cwd</TT></FONT></FONT><FONT COLOR=black><FONT SIZE=3> that identifies a file is called a </FONT></FONT><FONT COLOR=black><FONT SIZE=3><B>path</B></FONT></FONT><FONT COLOR=black><FONT SIZE=3>.
| | user named <TT>dinsdale</TT>. |
| A </FONT></FONT><FONT COLOR=black><FONT SIZE=3><B>relative path</B></FONT></FONT><FONT COLOR=black><FONT SIZE=3> starts from the current directory; | | |
| an </FONT></FONT><FONT COLOR=black><FONT SIZE=3><B>absolute path</B></FONT></FONT><FONT COLOR=black><FONT SIZE=3> starts from the topmost directory in the | | |
| file system.</FONT></FONT></P><P><A NAME="@default1255"></A><FONT COLOR=black><FONT SIZE=3> | | |
| </FONT></FONT><A NAME="@default1256"></A><FONT COLOR=black><FONT SIZE=3>
| | |
| </FONT></FONT><A NAME="@default1257"></A><FONT COLOR=black><FONT SIZE=3>
| | A string like <TT>cwd</TT> that identifies a file is called a '''path'''. |
| </FONT></FONT><A NAME="@default1258"></A></P><P><FONT COLOR=black><FONT SIZE=3>The paths we have seen so far are simple filenames, so they are
| | A '''relative path''' starts from the current directory; |
| | an '''absolute path''' starts from the topmost directory in the |
| | file system. |
| | |
| | |
| | |
| | |
| | |
| | |
| | The paths we have seen so far are simple filenames, so they are |
| relative to the current directory. To find the absolute path to | | relative to the current directory. To find the absolute path to |
| a file, you can use </FONT></FONT><FONT COLOR=black><FONT SIZE=3><TT>os.path.abspath</TT></FONT></FONT><FONT COLOR=black><FONT SIZE=3>:</FONT></FONT></P><PRE CLASS="verbatim"><FONT COLOR=blue><FONT SIZE=4>>>> os.path.abspath('memo.txt') | | a file, you can use <TT>os.path.abspath</TT>: |
| | <PRE CLASS="verbatim">>>> os.path.abspath('memo.txt') |
| '/home/dinsdale/memo.txt' | | '/home/dinsdale/memo.txt' |
| </FONT></FONT></PRE><P><FONT COLOR=black><FONT SIZE=3><TT>os.path.exists</TT></FONT></FONT><FONT COLOR=black><FONT SIZE=3> checks
| | </PRE> |
| whether a file or directory exists:</FONT></FONT></P><P><A NAME="@default1259"></A><FONT COLOR=black><FONT SIZE=3> | | <TT>os.path.exists</TT> checks |
| </FONT></FONT><A NAME="@default1260"></A></P><PRE CLASS="verbatim"><FONT COLOR=blue><FONT SIZE=4>>>> os.path.exists('memo.txt')
| | whether a file or directory exists: |
| | |
| | |
| | |
| | <PRE CLASS="verbatim">>>> os.path.exists('memo.txt') |
| True | | True |
| </FONT></FONT></PRE><P><FONT COLOR=black><FONT SIZE=3>If it exists, </FONT></FONT><FONT COLOR=black><FONT SIZE=3><TT>os.path.isdir</TT></FONT></FONT><FONT COLOR=black><FONT SIZE=3> checks whether it’s a directory:</FONT></FONT></P><PRE CLASS="verbatim"><FONT COLOR=blue><FONT SIZE=4>>>> os.path.isdir('memo.txt')
| | </PRE> |
| | If it exists, <TT>os.path.isdir</TT> checks whether it’s a directory: |
| | <PRE CLASS="verbatim">>>> os.path.isdir('memo.txt') |
| False | | False |
| >>> os.path.isdir('music') | | >>> os.path.isdir('music') |
| True | | True |
| </FONT></FONT></PRE><P><FONT COLOR=black><FONT SIZE=3>Similarly, </FONT></FONT><FONT COLOR=black><FONT SIZE=3><TT>os.path.isfile</TT></FONT></FONT><FONT COLOR=black><FONT SIZE=3> checks whether it’s a file.</FONT></FONT></P><P><FONT COLOR=black><FONT SIZE=3><TT>os.listdir</TT></FONT></FONT><FONT COLOR=black><FONT SIZE=3> returns a list of the files (and other directories)
| | </PRE> |
| in the given directory:</FONT></FONT></P><PRE CLASS="verbatim"><FONT COLOR=blue><FONT SIZE=4>>>> os.listdir(cwd) | | Similarly, <TT>os.path.isfile</TT> checks whether it’s a file. |
| | |
| | <TT>os.listdir</TT> returns a list of the files (and other directories) |
| | in the given directory: |
| | <PRE CLASS="verbatim">>>> os.listdir(cwd) |
| ['music', 'photos', 'memo.txt'] | | ['music', 'photos', 'memo.txt'] |
| </FONT></FONT></PRE><P><FONT COLOR=black><FONT SIZE=3>To demonstrate these functions, the following example
| | </PRE> |
| | To demonstrate these functions, the following example |
| “walks” through a directory, prints | | “walks” through a directory, prints |
| the names of all the files, and calls itself recursively on | | the names of all the files, and calls itself recursively on |
| all the directories.</FONT></FONT></P><P><A NAME="@default1261"></A><FONT COLOR=black><FONT SIZE=3> | | all the directories. |
| </FONT></FONT><A NAME="@default1262"></A></P><PRE CLASS="verbatim"><FONT COLOR=blue><FONT SIZE=4>def walk(dir):
| | |
| | |
| | |
| | <PRE CLASS="verbatim">def walk(dir): |
| for name in os.listdir(dir): | | for name in os.listdir(dir): |
| path = os.path.join(dir, name) | | path = os.path.join(dir, name) |
| Line 131: |
Line 213: |
| else: | | else: |
| walk(path) | | walk(path) |
| </FONT></FONT></PRE><P><FONT COLOR=black><FONT SIZE=3><TT>os.path.join</TT></FONT></FONT><FONT COLOR=black><FONT SIZE=3> takes a directory and a file name and joins
| | </PRE> |
| them into a complete path. </FONT></FONT></P><DIV CLASS="theorem"><FONT COLOR=black><FONT SIZE=3><B>Exercise 1</B></FONT></FONT><FONT COLOR=black><FONT SIZE=3>  <EM> | | <TT>os.path.join</TT> takes a directory and a file name and joins |
| Modify </EM></FONT></FONT><FONT COLOR=black><FONT SIZE=3><EM><TT>walk</TT></EM></FONT></FONT><FONT COLOR=black><FONT SIZE=3><EM> so that instead of printing the names of | | them into a complete path. |
| | <DIV CLASS="theorem">'''Exercise 1'''  '' |
| | Modify ''''<TT>walk</TT>'''' so that instead of printing the names of |
| the files, it returns a list of names. | | the files, it returns a list of names. |
| </EM></FONT></FONT></DIV><DIV CLASS="theorem"><FONT COLOR=black><FONT SIZE=3><B>Exercise 2</B></FONT></FONT><FONT COLOR=black><FONT SIZE=3>  <EM>
| | ''</DIV><DIV CLASS="theorem">'''Exercise 2'''  '' |
| The </EM></FONT></FONT><FONT COLOR=black><FONT SIZE=3><EM><TT>os</TT></EM></FONT></FONT><FONT COLOR=black><FONT SIZE=3><EM> module provides a function called </EM></FONT></FONT><FONT COLOR=black><FONT SIZE=3><EM><TT>walk</TT></EM></FONT></FONT><FONT COLOR=black><FONT SIZE=3><EM> | | The ''''<TT>os</TT>'''' module provides a function called ''''<TT>walk</TT>'''' |
| that is similar to this one but more versatile. Read | | that is similar to this one but more versatile. Read |
| the documentation and use it to print the names of the | | the documentation and use it to print the names of the |
| files in a given directory and its subdirectories. | | files in a given directory and its subdirectories. |
| </EM></FONT></FONT></DIV><H2 CLASS="section"><A NAME="toc157"></A><A NAME="htoc173"><FONT COLOR=black><FONT SIZE=3>14.5</FONT></FONT></A><FONT COLOR=black><FONT SIZE=3>  Catching exceptions</FONT></FONT></H2><P><FONT COLOR=black><FONT SIZE=3>
| | ''</DIV>=== 14.5  Catching exceptions === |
| </FONT></FONT><A NAME="catch"></A></P><P><FONT COLOR=black><FONT SIZE=3>A lot of things can go wrong when you try to read and write
| | |
| | |
| | |
| | |
| | A lot of things can go wrong when you try to read and write |
| files. If you try to open a file that doesn’t exist, you get an | | files. If you try to open a file that doesn’t exist, you get an |
| </FONT></FONT><FONT COLOR=black><FONT SIZE=3><TT>IOError</TT></FONT></FONT><FONT COLOR=black><FONT SIZE=3>:</FONT></FONT></P><P><A NAME="@default1263"></A><FONT COLOR=black><FONT SIZE=3>
| | <TT>IOError</TT>: |
| </FONT></FONT><A NAME="@default1264"></A><FONT COLOR=black><FONT SIZE=3>
| | |
| </FONT></FONT><A NAME="@default1265"></A><FONT COLOR=black><FONT SIZE=3>
| | |
| </FONT></FONT><A NAME="@default1266"></A></P><PRE CLASS="verbatim"><FONT COLOR=blue><FONT SIZE=4>>>> fin = open('bad_file')
| | |
| | |
| | |
| | <PRE CLASS="verbatim">>>> fin = open('bad_file') |
| IOError: [Errno 2] No such file or directory: 'bad_file' | | IOError: [Errno 2] No such file or directory: 'bad_file' |
| </FONT></FONT></PRE><P><FONT COLOR=black><FONT SIZE=3>If you don’t have permission to access a file:</FONT></FONT></P><P><A NAME="@default1267"></A><FONT COLOR=black><FONT SIZE=3>
| | </PRE> |
| </FONT></FONT><A NAME="@default1268"></A></P><PRE CLASS="verbatim"><FONT COLOR=blue><FONT SIZE=4>>>> fout = open('/etc/passwd', 'w')
| | If you don’t have permission to access a file: |
| | |
| | |
| | |
| | <PRE CLASS="verbatim">>>> fout = open('/etc/passwd', 'w') |
| IOError: [Errno 13] Permission denied: '/etc/passwd' | | IOError: [Errno 13] Permission denied: '/etc/passwd' |
| </FONT></FONT></PRE><P><FONT COLOR=black><FONT SIZE=3>And if you try to open a directory for reading, you get</FONT></FONT></P><PRE CLASS="verbatim"><FONT COLOR=blue><FONT SIZE=4>>>> fin = open('/home')
| | </PRE> |
| | And if you try to open a directory for reading, you get |
| | <PRE CLASS="verbatim">>>> fin = open('/home') |
| IOError: [Errno 21] Is a directory | | IOError: [Errno 21] Is a directory |
| </FONT></FONT></PRE><P><FONT COLOR=black><FONT SIZE=3>To avoid these errors, you could use functions like </FONT></FONT><FONT COLOR=black><FONT SIZE=3><TT>os.path.exists</TT></FONT></FONT><FONT COLOR=black><FONT SIZE=3>
| | </PRE> |
| and </FONT></FONT><FONT COLOR=black><FONT SIZE=3><TT>os.path.isfile</TT></FONT></FONT><FONT COLOR=black><FONT SIZE=3>, but it would take a lot of time and code | | To avoid these errors, you could use functions like <TT>os.path.exists</TT> |
| to check all the possibilities (if “</FONT></FONT><FONT COLOR=black><FONT SIZE=3><TT>Errno 21</TT></FONT></FONT><FONT COLOR=black><FONT SIZE=3>” is any | | and <TT>os.path.isfile</TT>, but it would take a lot of time and code |
| indication, there are at least 21 things that can go wrong).</FONT></FONT></P><P><A NAME="@default1269"></A><FONT COLOR=black><FONT SIZE=3> | | to check all the possibilities (if “<TT>Errno 21</TT>” is any |
| </FONT></FONT><A NAME="@default1270"></A><FONT COLOR=black><FONT SIZE=3>
| | indication, there are at least 21 things that can go wrong). |
| </FONT></FONT><A NAME="@default1271"></A></P><P><FONT COLOR=black><FONT SIZE=3>It is better to go ahead and try, and deal with problems if they
| | |
| happen, which is exactly what the </FONT></FONT><FONT COLOR=black><FONT SIZE=3><TT>try</TT></FONT></FONT><FONT COLOR=black><FONT SIZE=3> statement does. The | | |
| syntax is similar to an </FONT></FONT><FONT COLOR=black><FONT SIZE=3><TT>if</TT></FONT></FONT><FONT COLOR=black><FONT SIZE=3> statement:</FONT></FONT></P><PRE CLASS="verbatim"><FONT COLOR=blue><FONT SIZE=4>try: | | |
| | |
| | |
| | It is better to go ahead and try, and deal with problems if they |
| | happen, which is exactly what the <TT>try</TT> statement does. The |
| | syntax is similar to an <TT>if</TT> statement: |
| | <PRE CLASS="verbatim">try: |
| fin = open('bad_file') | | fin = open('bad_file') |
| for line in fin: | | for line in fin: |
| Line 167: |
Line 270: |
| except: | | except: |
| print 'Something went wrong.' | | print 'Something went wrong.' |
| </FONT></FONT></PRE><P><FONT COLOR=black><FONT SIZE=3>Python starts by executing the </FONT></FONT><FONT COLOR=black><FONT SIZE=3><TT>try</TT></FONT></FONT><FONT COLOR=black><FONT SIZE=3> clause. If all goes
| | </PRE> |
| well, it skips the </FONT></FONT><FONT COLOR=black><FONT SIZE=3><TT>except</TT></FONT></FONT><FONT COLOR=black><FONT SIZE=3> clause and proceeds. If an | | Python starts by executing the <TT>try</TT> clause. If all goes |
| exception occurs, it jumps out of the </FONT></FONT><FONT COLOR=black><FONT SIZE=3><TT>try</TT></FONT></FONT><FONT COLOR=black><FONT SIZE=3> clause and | | well, it skips the <TT>except</TT> clause and proceeds. If an |
| executes the </FONT></FONT><FONT COLOR=black><FONT SIZE=3><TT>except</TT></FONT></FONT><FONT COLOR=black><FONT SIZE=3> clause.</FONT></FONT></P><P><FONT COLOR=black><FONT SIZE=3>Handling an exception with a </FONT></FONT><FONT COLOR=black><FONT SIZE=3><TT>try</TT></FONT></FONT><FONT COLOR=black><FONT SIZE=3> statement is called </FONT></FONT><FONT COLOR=black><FONT SIZE=3><B>catching</B></FONT></FONT><FONT COLOR=black><FONT SIZE=3> an exception. In this example, the </FONT></FONT><FONT COLOR=black><FONT SIZE=3><TT>except</TT></FONT></FONT><FONT COLOR=black><FONT SIZE=3> clause | | exception occurs, it jumps out of the <TT>try</TT> clause and |
| | executes the <TT>except</TT> clause. |
| | |
| | Handling an exception with a <TT>try</TT> statement is called '''catching''' an exception. In this example, the <TT>except</TT> clause |
| prints an error message that is not very helpful. In general, | | prints an error message that is not very helpful. In general, |
| catching an exception gives you a chance to fix the problem, or try | | catching an exception gives you a chance to fix the problem, or try |
| again, or at least end the program gracefully.</FONT></FONT></P><H2 CLASS="section"><A NAME="toc158"></A><A NAME="htoc174"><FONT COLOR=black><FONT SIZE=3>14.6</FONT></FONT></A><FONT COLOR=black><FONT SIZE=3>  Databases</FONT></FONT></H2><P><A NAME="@default1272"></A></P><P><FONT COLOR=black><FONT SIZE=3>A </FONT></FONT><FONT COLOR=black><FONT SIZE=3><B>database</B></FONT></FONT><FONT COLOR=black><FONT SIZE=3> is a file that is organized for storing data. | | again, or at least end the program gracefully. |
| | === 14.6  Databases === |
| | |
| | A '''database''' is a file that is organized for storing data. |
| Most databases are organized like a dictionary in the sense | | Most databases are organized like a dictionary in the sense |
| that they map from keys to values. The biggest difference | | that they map from keys to values. The biggest difference |
| is that the database is on disk (or other permanent storage), | | is that the database is on disk (or other permanent storage), |
| so it persists after the program ends.</FONT></FONT></P><P><A NAME="@default1273"></A><FONT COLOR=black><FONT SIZE=3> | | so it persists after the program ends. |
| </FONT></FONT><A NAME="@default1274"></A></P><P><FONT COLOR=black><FONT SIZE=3>The module </FONT></FONT><FONT COLOR=black><FONT SIZE=3><TT>anydbm</TT></FONT></FONT><FONT COLOR=black><FONT SIZE=3> provides an interface for creating
| | |
| | |
| | |
| | |
| | The module <TT>anydbm</TT> provides an interface for creating |
| and updating database files. As an example, I’ll create a database | | and updating database files. As an example, I’ll create a database |
| that contains captions for image files.</FONT></FONT></P><P><A NAME="@default1275"></A><FONT COLOR=black><FONT SIZE=3> | | that contains captions for image files. |
| </FONT></FONT><A NAME="@default1276"></A></P><P><FONT COLOR=black><FONT SIZE=3>Opening a database is similar
| | |
| to opening other files:</FONT></FONT></P><PRE CLASS="verbatim"><FONT COLOR=blue><FONT SIZE=4>>>> import anydbm | | |
| | |
| | |
| | Opening a database is similar |
| | to opening other files: |
| | <PRE CLASS="verbatim">>>> import anydbm |
| >>> db = anydbm.open('captions.db', 'c') | | >>> db = anydbm.open('captions.db', 'c') |
| </FONT></FONT></PRE><P><FONT COLOR=black><FONT SIZE=3>The mode </FONT></FONT><CODE><FONT COLOR=black><FONT SIZE=3>'c'</FONT></FONT></CODE><FONT COLOR=black><FONT SIZE=3> means that the database should be created if
| | </PRE> |
| | The mode <CODE>'c'</CODE> means that the database should be created if |
| it doesn’t already exist. The result is a database object | | it doesn’t already exist. The result is a database object |
| that can be used (for most operations) like a dictionary. | | that can be used (for most operations) like a dictionary. |
| If you create a new item, </FONT></FONT><FONT COLOR=black><FONT SIZE=3><TT>anydbm</TT></FONT></FONT><FONT COLOR=black><FONT SIZE=3> updates the database file.</FONT></FONT></P><P><A NAME="@default1277"></A></P><PRE CLASS="verbatim"><FONT COLOR=blue><FONT SIZE=4>>>> db['cleese.png'] = 'Photo of John Cleese.' | | If you create a new item, <TT>anydbm</TT> updates the database file. |
| </FONT></FONT></PRE><P><FONT COLOR=black><FONT SIZE=3>When you access one of the items, </FONT></FONT><FONT COLOR=black><FONT SIZE=3><TT>anydbm</TT></FONT></FONT><FONT COLOR=black><FONT SIZE=3> reads the file:</FONT></FONT></P><PRE CLASS="verbatim"><FONT COLOR=blue><FONT SIZE=4>>>> print db['cleese.png']
| | |
| | <PRE CLASS="verbatim">>>> db['cleese.png'] = 'Photo of John Cleese.' |
| | </PRE> |
| | When you access one of the items, <TT>anydbm</TT> reads the file: |
| | <PRE CLASS="verbatim">>>> print db['cleese.png'] |
| Photo of John Cleese. | | Photo of John Cleese. |
| </FONT></FONT></PRE><P><FONT COLOR=black><FONT SIZE=3>If you make another assignment to an existing key, </FONT></FONT><FONT COLOR=black><FONT SIZE=3><TT>anydbm</TT></FONT></FONT><FONT COLOR=black><FONT SIZE=3> replaces
| | </PRE> |
| the old value:</FONT></FONT></P><PRE CLASS="verbatim"><FONT COLOR=blue><FONT SIZE=4>>>> db['cleese.png'] = 'Photo of John Cleese doing a silly walk.' | | If you make another assignment to an existing key, <TT>anydbm</TT> replaces |
| | the old value: |
| | <PRE CLASS="verbatim">>>> db['cleese.png'] = 'Photo of John Cleese doing a silly walk.' |
| >>> print db['cleese.png'] | | >>> print db['cleese.png'] |
| Photo of John Cleese doing a silly walk. | | Photo of John Cleese doing a silly walk. |
| </FONT></FONT></PRE><P><FONT COLOR=black><FONT SIZE=3>Many dictionary methods, like </FONT></FONT><FONT COLOR=black><FONT SIZE=3><TT>keys</TT></FONT></FONT><FONT COLOR=black><FONT SIZE=3> and </FONT></FONT><FONT COLOR=black><FONT SIZE=3><TT>items</TT></FONT></FONT><FONT COLOR=black><FONT SIZE=3>, also
| | </PRE> |
| work with database objects. So does iteration with a </FONT></FONT><FONT COLOR=black><FONT SIZE=3><TT>for</TT></FONT></FONT><FONT COLOR=black><FONT SIZE=3> | | Many dictionary methods, like <TT>keys</TT> and <TT>items</TT>, also |
| statement.</FONT></FONT></P><P><A NAME="@default1278"></A></P><PRE CLASS="verbatim"><FONT COLOR=blue><FONT SIZE=4>for key in db: | | work with database objects. So does iteration with a <TT>for</TT> |
| | statement. |
| | |
| | <PRE CLASS="verbatim">for key in db: |
| print key | | print key |
| </FONT></FONT></PRE><P><FONT COLOR=black><FONT SIZE=3>As with other files, you should close the database when you are
| | </PRE> |
| done:</FONT></FONT></P><PRE CLASS="verbatim"><FONT COLOR=blue><FONT SIZE=4>>>> db.close() | | As with other files, you should close the database when you are |
| </FONT></FONT></PRE><P><A NAME="@default1279"></A><FONT COLOR=black><FONT SIZE=3>
| | done: |
| </FONT></FONT><A NAME="@default1280"></A></P><H2 CLASS="section"><A NAME="toc159"></A><A NAME="htoc175"><FONT COLOR=black><FONT SIZE=3>14.7</FONT></FONT></A><FONT COLOR=black><FONT SIZE=3>  Pickling</FONT></FONT></H2><P><A NAME="@default1281"></A></P><P><FONT COLOR=black><FONT SIZE=3>A limitation of </FONT></FONT><FONT COLOR=black><FONT SIZE=3><TT>anydbm</TT></FONT></FONT><FONT COLOR=black><FONT SIZE=3> is that the keys and values have
| | <PRE CLASS="verbatim">>>> db.close() |
| | </PRE> |
| | |
| | |
| | === 14.7  Pickling === |
| | |
| | A limitation of <TT>anydbm</TT> is that the keys and values have |
| to be strings. If you try to use any other type, you get an | | to be strings. If you try to use any other type, you get an |
| error.</FONT></FONT></P><P><A NAME="@default1282"></A><FONT COLOR=black><FONT SIZE=3> | | error. |
| </FONT></FONT><A NAME="@default1283"></A></P><P><FONT COLOR=black><FONT SIZE=3>The </FONT></FONT><FONT COLOR=black><FONT SIZE=3><TT>pickle</TT></FONT></FONT><FONT COLOR=black><FONT SIZE=3> module can help. It translates
| | |
| | |
| | |
| | |
| | The <TT>pickle</TT> module can help. It translates |
| almost any type of object into a string suitable for storage in a | | almost any type of object into a string suitable for storage in a |
| database, and then translates strings back into objects.</FONT></FONT></P><P><FONT COLOR=black><FONT SIZE=3><TT>pickle.dumps</TT></FONT></FONT><FONT COLOR=black><FONT SIZE=3> takes an object as a parameter and returns | | database, and then translates strings back into objects. |
| a string representation (</FONT></FONT><FONT COLOR=black><FONT SIZE=3><TT>dumps</TT></FONT></FONT><FONT COLOR=black><FONT SIZE=3> is short for “dump string”):</FONT></FONT></P><PRE CLASS="verbatim"><FONT COLOR=blue><FONT SIZE=4>>>> import pickle | | |
| | <TT>pickle.dumps</TT> takes an object as a parameter and returns |
| | a string representation (<TT>dumps</TT> is short for “dump string”): |
| | <PRE CLASS="verbatim">>>> import pickle |
| >>> t = [1, 2, 3] | | >>> t = [1, 2, 3] |
| >>> pickle.dumps(t) | | >>> pickle.dumps(t) |
| '(lp0\nI1\naI2\naI3\na.' | | '(lp0\nI1\naI2\naI3\na.' |
| </FONT></FONT></PRE><P><FONT COLOR=black><FONT SIZE=3>The format isn’t obvious to human readers; it is meant to be
| | </PRE> |
| easy for </FONT></FONT><FONT COLOR=black><FONT SIZE=3><TT>pickle</TT></FONT></FONT><FONT COLOR=black><FONT SIZE=3> to interpret. </FONT></FONT><FONT COLOR=black><FONT SIZE=3><TT>pickle.loads</TT></FONT></FONT><FONT COLOR=black><FONT SIZE=3> | | The format isn’t obvious to human readers; it is meant to be |
| (“load string”) reconstitutes the object:</FONT></FONT></P><PRE CLASS="verbatim"><FONT COLOR=blue><FONT SIZE=4>>>> t1 = [1, 2, 3] | | easy for <TT>pickle</TT> to interpret. <TT>pickle.loads</TT> |
| | (“load string”) reconstitutes the object: |
| | <PRE CLASS="verbatim">>>> t1 = [1, 2, 3] |
| >>> s = pickle.dumps(t1) | | >>> s = pickle.dumps(t1) |
| >>> t2 = pickle.loads(s) | | >>> t2 = pickle.loads(s) |
| >>> print t2 | | >>> print t2 |
| [1, 2, 3] | | [1, 2, 3] |
| </FONT></FONT></PRE><P><FONT COLOR=black><FONT SIZE=3>Although the new object has the same value as the old, it is
| | </PRE> |
| not (in general) the same object:</FONT></FONT></P><PRE CLASS="verbatim"><FONT COLOR=blue><FONT SIZE=4>>>> t == t2 | | Although the new object has the same value as the old, it is |
| | not (in general) the same object: |
| | <PRE CLASS="verbatim">>>> t == t2 |
| True | | True |
| >>> t is t2 | | >>> t is t2 |
| False | | False |
| </FONT></FONT></PRE><P><FONT COLOR=black><FONT SIZE=3>In other words, pickling and then unpickling has the same effect
| | </PRE> |
| as copying the object.</FONT></FONT></P><P><FONT COLOR=black><FONT SIZE=3>You can use </FONT></FONT><FONT COLOR=black><FONT SIZE=3><TT>pickle</TT></FONT></FONT><FONT COLOR=black><FONT SIZE=3> to store non-strings in a database. | | In other words, pickling and then unpickling has the same effect |
| | as copying the object. |
| | |
| | You can use <TT>pickle</TT> to store non-strings in a database. |
| In fact, this combination is so common that it has been | | In fact, this combination is so common that it has been |
| encapsulated in a module called </FONT></FONT><FONT COLOR=black><FONT SIZE=3><TT>shelve</TT></FONT></FONT><FONT COLOR=black><FONT SIZE=3>. </FONT></FONT></P><P><A NAME="@default1284"></A><FONT COLOR=black><FONT SIZE=3> | | encapsulated in a module called <TT>shelve</TT>. |
| </FONT></FONT><A NAME="@default1285"></A></P><DIV CLASS="theorem"><FONT COLOR=black><FONT SIZE=3><B>Exercise 3</B></FONT></FONT><FONT COLOR=black><FONT SIZE=3>  </FONT></FONT><P><A NAME="@default1286"></A><FONT COLOR=black><FONT SIZE=3><EM>
| | |
| </EM></FONT></FONT><A NAME="@default1287"></A></P><P><FONT COLOR=black><FONT SIZE=3><EM>If you did Exercise </EM></FONT></FONT><A HREF="book013.html#anagrams"><FONT COLOR=black><FONT SIZE=3><EM>12.4</EM></FONT></FONT></A><FONT COLOR=black><FONT SIZE=3><EM>, modify your solution so that
| | |
| | |
| | <DIV CLASS="theorem">'''Exercise 3'''   |
| | '' |
| | '' |
| | |
| | ''If you did Exercise ''''12.4'''', modify your solution so that |
| it creates a database that maps from each word in the list to | | it creates a database that maps from each word in the list to |
| a list of words that use the same set of letters.</EM></FONT></FONT></P><P><FONT COLOR=black><FONT SIZE=3><EM>Write a different program that opens the database and prints | | a list of words that use the same set of letters.'' |
| | |
| | ''Write a different program that opens the database and prints |
| the contents in a human-readable format. | | the contents in a human-readable format. |
| </EM></FONT></FONT></P></DIV><H2 CLASS="section"><A NAME="toc160"></A><A NAME="htoc176"><FONT COLOR=black><FONT SIZE=3>14.8</FONT></FONT></A><FONT COLOR=black><FONT SIZE=3>  Pipes</FONT></FONT></H2><P><A NAME="@default1288"></A><FONT COLOR=black><FONT SIZE=3>
| | '' |
| </FONT></FONT><A NAME="@default1289"></A></P><P><FONT COLOR=black><FONT SIZE=3>Most operating systems provide a command-line interface,
| | </DIV>=== 14.8  Pipes === |
| also known as a </FONT></FONT><FONT COLOR=black><FONT SIZE=3><B>shell</B></FONT></FONT><FONT COLOR=black><FONT SIZE=3>. Shells usually provide commands | | |
| | |
| | |
| | |
| | Most operating systems provide a command-line interface, |
| | also known as a '''shell'''. Shells usually provide commands |
| to navigate the file system and launch applications. For | | to navigate the file system and launch applications. For |
| example, in Unix, you can change directories with </FONT></FONT><FONT COLOR=black><FONT SIZE=3><TT>cd</TT></FONT></FONT><FONT COLOR=black><FONT SIZE=3>, | | example, in Unix, you can change directories with <TT>cd</TT>, |
| display the contents of a directory with </FONT></FONT><FONT COLOR=black><FONT SIZE=3><TT>ls</TT></FONT></FONT><FONT COLOR=black><FONT SIZE=3>, and launch | | display the contents of a directory with <TT>ls</TT>, and launch |
| a web browser by typing (for example) </FONT></FONT><FONT COLOR=black><FONT SIZE=3><TT>firefox</TT></FONT></FONT><FONT COLOR=black><FONT SIZE=3>.</FONT></FONT></P><P><A NAME="@default1290"></A><FONT COLOR=black><FONT SIZE=3> | | a web browser by typing (for example) <TT>firefox</TT>. |
| </FONT></FONT><A NAME="@default1291"></A></P><P><FONT COLOR=black><FONT SIZE=3>Any program that you can launch from the shell can also be
| | |
| launched from Python using a </FONT></FONT><FONT COLOR=black><FONT SIZE=3><B>pipe</B></FONT></FONT><FONT COLOR=black><FONT SIZE=3>. A pipe is an object | | |
| that represents a running process.</FONT></FONT></P><P><FONT COLOR=black><FONT SIZE=3>For example, the Unix command </FONT></FONT><FONT COLOR=black><FONT SIZE=3><TT>ls -l</TT></FONT></FONT><FONT COLOR=black><FONT SIZE=3> normally displays the | | |
| | |
| | Any program that you can launch from the shell can also be |
| | launched from Python using a '''pipe'''. A pipe is an object |
| | that represents a running process. |
| | |
| | For example, the Unix command <TT>ls -l</TT> normally displays the |
| contents of the current directory (in long format). You can | | contents of the current directory (in long format). You can |
| launch </FONT></FONT><FONT COLOR=black><FONT SIZE=3><TT>ls</TT></FONT></FONT><FONT COLOR=black><FONT SIZE=3> with </FONT></FONT><FONT COLOR=black><FONT SIZE=3><TT>os.popen</TT></FONT></FONT><FONT COLOR=black><FONT SIZE=3>:</FONT></FONT></P><P><A NAME="@default1292"></A><FONT COLOR=black><FONT SIZE=3> | | launch <TT>ls</TT> with <TT>os.popen</TT>: |
| </FONT></FONT><A NAME="@default1293"></A></P><PRE CLASS="verbatim"><FONT COLOR=blue><FONT SIZE=4>>>> cmd = 'ls -l'
| | |
| | |
| | |
| | <PRE CLASS="verbatim">>>> cmd = 'ls -l' |
| >>> fp = os.popen(cmd) | | >>> fp = os.popen(cmd) |
| </FONT></FONT></PRE><P><FONT COLOR=black><FONT SIZE=3>The argument is a string that contains a shell command. The
| | </PRE> |
| | The argument is a string that contains a shell command. The |
| return value is a file pointer that behaves just like an open | | return value is a file pointer that behaves just like an open |
| file. You can read the output from the </FONT></FONT><FONT COLOR=black><FONT SIZE=3><TT>ls</TT></FONT></FONT><FONT COLOR=black><FONT SIZE=3> process one | | file. You can read the output from the <TT>ls</TT> process one |
| line at a time with </FONT></FONT><FONT COLOR=black><FONT SIZE=3><TT>readline</TT></FONT></FONT><FONT COLOR=black><FONT SIZE=3> or get the whole thing at | | line at a time with <TT>readline</TT> or get the whole thing at |
| once with </FONT></FONT><FONT COLOR=black><FONT SIZE=3><TT>read</TT></FONT></FONT><FONT COLOR=black><FONT SIZE=3>:</FONT></FONT></P><P><A NAME="@default1294"></A><FONT COLOR=black><FONT SIZE=3> | | once with <TT>read</TT>: |
| </FONT></FONT><A NAME="@default1295"></A><FONT COLOR=black><FONT SIZE=3>
| | |
| </FONT></FONT><A NAME="@default1296"></A><FONT COLOR=black><FONT SIZE=3>
| | |
| </FONT></FONT><A NAME="@default1297"></A></P><PRE CLASS="verbatim"><FONT COLOR=blue><FONT SIZE=4>>>> res = fp.read()
| | |
| </FONT></FONT></PRE><P><FONT COLOR=black><FONT SIZE=3>When you are done, you close the pipe like a file:</FONT></FONT></P><P><A NAME="@default1298"></A><FONT COLOR=black><FONT SIZE=3>
| | |
| </FONT></FONT><A NAME="@default1299"></A></P><PRE CLASS="verbatim"><FONT COLOR=blue><FONT SIZE=4>>>> stat = fp.close()
| | |
| | <PRE CLASS="verbatim">>>> res = fp.read() |
| | </PRE> |
| | When you are done, you close the pipe like a file: |
| | |
| | |
| | |
| | <PRE CLASS="verbatim">>>> stat = fp.close() |
| >>> print stat | | >>> print stat |
| None | | None |
| </FONT></FONT></PRE><P><FONT COLOR=black><FONT SIZE=3>The return value is the final status of the </FONT></FONT><FONT COLOR=black><FONT SIZE=3><TT>ls</TT></FONT></FONT><FONT COLOR=black><FONT SIZE=3> process;
| | </PRE> |
| </FONT></FONT><FONT COLOR=black><FONT SIZE=3><TT>None</TT></FONT></FONT><FONT COLOR=black><FONT SIZE=3> means that it ended normally (with no errors).</FONT></FONT></P><P><A NAME="@default1300"></A><FONT COLOR=black><FONT SIZE=3>
| | The return value is the final status of the <TT>ls</TT> process; |
| </FONT></FONT><A NAME="@default1301"></A><FONT COLOR=black><FONT SIZE=3>
| | <TT>None</TT> means that it ended normally (with no errors). |
| </FONT></FONT><A NAME="@default1302"></A><FONT COLOR=black><FONT SIZE=3>
| | |
| </FONT></FONT><A NAME="@default1303"></A></P><P><FONT COLOR=black><FONT SIZE=3>A common use of pipes is to read a compressed file incrementally;
| | |
| | |
| | |
| | |
| | |
| | A common use of pipes is to read a compressed file incrementally; |
| that is, without uncompressing the whole thing at once. The | | that is, without uncompressing the whole thing at once. The |
| following function takes the name of a compressed file as a | | following function takes the name of a compressed file as a |
| parameter and returns a pipe that uses </FONT></FONT><FONT COLOR=black><FONT SIZE=3><TT>gzip</TT></FONT></FONT><FONT COLOR=black><FONT SIZE=3> to decompress | | parameter and returns a pipe that uses <TT>gzip</TT> to decompress |
| the contents:</FONT></FONT></P><PRE CLASS="verbatim"><FONT COLOR=blue><FONT SIZE=4>def open_gzip(filename): | | the contents: |
| | <PRE CLASS="verbatim">def open_gzip(filename): |
| cmd = 'gunzip -c ' + filename | | cmd = 'gunzip -c ' + filename |
| fp = os.popen(cmd) | | fp = os.popen(cmd) |
| return fp | | return fp |
| </FONT></FONT></PRE><P><FONT COLOR=black><FONT SIZE=3>If you read lines from </FONT></FONT><FONT COLOR=black><FONT SIZE=3><TT>fp</TT></FONT></FONT><FONT COLOR=black><FONT SIZE=3> one at a time, you never have
| | </PRE> |
| to store the uncompressed file in memory or on disk.</FONT></FONT></P><H2 CLASS="section"><A NAME="toc161"></A><A NAME="htoc177"><FONT COLOR=black><FONT SIZE=3>14.9</FONT></FONT></A><FONT COLOR=black><FONT SIZE=3>  Writing modules</FONT></FONT></H2><P><FONT COLOR=black><FONT SIZE=3> | | If you read lines from <TT>fp</TT> one at a time, you never have |
| </FONT></FONT><A NAME="modules"></A></P><P><A NAME="@default1304"></A><FONT COLOR=black><FONT SIZE=3>
| | to store the uncompressed file in memory or on disk. |
| </FONT></FONT><A NAME="@default1305"></A></P><P><FONT COLOR=black><FONT SIZE=3>Any file that contains Python code can be imported as a module.
| | === 14.9  Writing modules === |
| For example, suppose you have a file named </FONT></FONT><FONT COLOR=black><FONT SIZE=3><TT>wc.py</TT></FONT></FONT><FONT COLOR=black><FONT SIZE=3> with the following | | |
| code:</FONT></FONT></P><PRE CLASS="verbatim"><FONT COLOR=blue><FONT SIZE=4>def linecount(filename): | | |
| | |
| | |
| | |
| | |
| | |
| | Any file that contains Python code can be imported as a module. |
| | For example, suppose you have a file named <TT>wc.py</TT> with the following |
| | code: |
| | <PRE CLASS="verbatim">def linecount(filename): |
| count = 0 | | count = 0 |
| for line in open(filename): | | for line in open(filename): |
| Line 282: |
Line 475: |
|
| |
|
| print linecount('wc.py') | | print linecount('wc.py') |
| </FONT></FONT></PRE><P><FONT COLOR=black><FONT SIZE=3>If you run this program, it reads itself and prints the number
| | </PRE> |
| | If you run this program, it reads itself and prints the number |
| of lines in the file, which is 7. | | of lines in the file, which is 7. |
| You can also import it like this:</FONT></FONT></P><PRE CLASS="verbatim"><FONT COLOR=blue><FONT SIZE=4>>>> import wc | | You can also import it like this: |
| | <PRE CLASS="verbatim">>>> import wc |
| 7 | | 7 |
| </FONT></FONT></PRE><P><FONT COLOR=black><FONT SIZE=3>Now you have a module object </FONT></FONT><FONT COLOR=black><FONT SIZE=3><TT>wc</TT></FONT></FONT><FONT COLOR=black><FONT SIZE=3>:</FONT></FONT></P><P><A NAME="@default1306"></A><FONT COLOR=black><FONT SIZE=3>
| | </PRE> |
| </FONT></FONT><A NAME="@default1307"></A></P><PRE CLASS="verbatim"><FONT COLOR=blue><FONT SIZE=4>>>> print wc
| | Now you have a module object <TT>wc</TT>: |
| | |
| | |
| | |
| | <PRE CLASS="verbatim">>>> print wc |
| <module 'wc' from 'wc.py'> | | <module 'wc' from 'wc.py'> |
| </FONT></FONT></PRE><P><FONT COLOR=black><FONT SIZE=3>That provides a function called </FONT></FONT><CODE><FONT COLOR=black><FONT SIZE=3>linecount</FONT></FONT></CODE><FONT COLOR=black><FONT SIZE=3>:</FONT></FONT></P><PRE CLASS="verbatim"><FONT COLOR=blue><FONT SIZE=4>>>> wc.linecount('wc.py')
| | </PRE> |
| | That provides a function called <CODE>linecount</CODE>: |
| | <PRE CLASS="verbatim">>>> wc.linecount('wc.py') |
| 7 | | 7 |
| </FONT></FONT></PRE><P><FONT COLOR=black><FONT SIZE=3>So that’s how you write modules in Python.</FONT></FONT></P><P><FONT COLOR=black><FONT SIZE=3>The only problem with this example is that when you import
| | </PRE> |
| | So that’s how you write modules in Python. |
| | |
| | The only problem with this example is that when you import |
| the module it executes the test code at the bottom. Normally | | the module it executes the test code at the bottom. Normally |
| when you import a module, it defines new functions but it | | when you import a module, it defines new functions but it |
| doesn’t execute them.</FONT></FONT></P><P><A NAME="@default1308"></A><FONT COLOR=black><FONT SIZE=3> | | doesn’t execute them. |
| </FONT></FONT><A NAME="@default1309"></A></P><P><FONT COLOR=black><FONT SIZE=3>Programs that will be imported as modules often
| | |
| use the following idiom:</FONT></FONT></P><PRE CLASS="verbatim"><FONT COLOR=blue><FONT SIZE=4>if __name__ == '__main__': | | |
| | |
| | |
| | Programs that will be imported as modules often |
| | use the following idiom: |
| | <PRE CLASS="verbatim">if __name__ == '__main__': |
| print linecount('wc.py') | | print linecount('wc.py') |
| </FONT></FONT></PRE><P><CODE><FONT COLOR=black><FONT SIZE=3>__name__</FONT></FONT></CODE><FONT COLOR=black><FONT SIZE=3> is a built-in variable that is set when the
| | </PRE> |
| | <CODE>__name__</CODE> is a built-in variable that is set when the |
| program starts. If the program is running as a script, | | program starts. If the program is running as a script, |
| </FONT></FONT><CODE><FONT COLOR=black><FONT SIZE=3>__name__</FONT></FONT></CODE><FONT COLOR=black><FONT SIZE=3> has the value </FONT></FONT><CODE><FONT COLOR=black><FONT SIZE=3>__main__</FONT></FONT></CODE><FONT COLOR=black><FONT SIZE=3>; in that
| | <CODE>__name__</CODE> has the value <CODE>__main__</CODE>; in that |
| case, the test code is executed. Otherwise, | | case, the test code is executed. Otherwise, |
| if the module is being imported, the test code is skipped.</FONT></FONT></P><DIV CLASS="theorem"><FONT COLOR=black><FONT SIZE=3><B>Exercise 4</B></FONT></FONT><FONT COLOR=black><FONT SIZE=3>  <EM> | | if the module is being imported, the test code is skipped. |
| Type this example into a file named </EM></FONT></FONT><FONT COLOR=black><FONT SIZE=3><EM><TT>wc.py</TT></EM></FONT></FONT><FONT COLOR=black><FONT SIZE=3><EM> and run | | <DIV CLASS="theorem">'''Exercise 4'''  '' |
| | Type this example into a file named ''''<TT>wc.py</TT>'''' and run |
| it as a script. Then run the Python interpreter and | | it as a script. Then run the Python interpreter and |
| </EM></FONT></FONT><FONT COLOR=black><FONT SIZE=3><EM><TT>import wc</TT></EM></FONT></FONT><FONT COLOR=black><FONT SIZE=3><EM>. What is the value of </EM></FONT></FONT><CODE><FONT COLOR=black><FONT SIZE=3><EM>__name__</EM></FONT></FONT></CODE><FONT COLOR=black><FONT SIZE=3><EM>
| | ''''<TT>import wc</TT>''''. What is the value of ''<CODE>''__name__''</CODE>'' |
| when the module is being imported?</EM></FONT></FONT><P><FONT COLOR=black><FONT SIZE=3><EM>Warning: If you import a module that has already been imported, | | when the module is being imported?'' |
| | ''Warning: If you import a module that has already been imported, |
| Python does nothing. It does not re-read the file, even if it has | | Python does nothing. It does not re-read the file, even if it has |
| changed.</EM></FONT></FONT></P><P><A NAME="@default1310"></A><FONT COLOR=black><FONT SIZE=3><EM> | | changed.'' |
| </EM></FONT></FONT><A NAME="@default1311"></A><FONT COLOR=black><FONT SIZE=3><EM>
| | |
| </EM></FONT></FONT><A NAME="@default1312"></A></P><P><FONT COLOR=black><FONT SIZE=3><EM>If you want to reload a module, you can use the built-in function
| | '' |
| </EM></FONT></FONT><FONT COLOR=black><FONT SIZE=3><EM><TT>reload</TT></EM></FONT></FONT><FONT COLOR=black><FONT SIZE=3><EM>, but it can be tricky, so the safest thing to do is
| | '''' |
| | '' |
| | |
| | ''If you want to reload a module, you can use the built-in function |
| | ''''<TT>reload</TT>'''', but it can be tricky, so the safest thing to do is |
| restart the interpreter and then import the module again. | | restart the interpreter and then import the module again. |
| </EM></FONT></FONT></P></DIV><H2 CLASS="section"><A NAME="toc162"></A><A NAME="htoc178"><FONT COLOR=black><FONT SIZE=3>14.10</FONT></FONT></A><FONT COLOR=black><FONT SIZE=3>  Debugging</FONT></FONT></H2><P><A NAME="@default1313"></A><FONT COLOR=black><FONT SIZE=3>
| | '' |
| </FONT></FONT><A NAME="@default1314"></A></P><P><FONT COLOR=black><FONT SIZE=3>When you are reading and writing files, you might run into problems
| | </DIV>=== 14.10  Debugging === |
| | |
| | |
| | |
| | |
| | When you are reading and writing files, you might run into problems |
| with whitespace. These errors can be hard to debug because spaces, | | with whitespace. These errors can be hard to debug because spaces, |
| tabs and newlines are normally invisible:</FONT></FONT></P><PRE CLASS="verbatim"><FONT COLOR=blue><FONT SIZE=4>>>> s = '1 2\t 3\n 4' | | tabs and newlines are normally invisible: |
| | <PRE CLASS="verbatim">>>> s = '1 2\t 3\n 4' |
| >>> print s | | >>> print s |
| 1 2 3 | | 1 2 3 |
| 4 | | 4 |
| </FONT></FONT></PRE><P><A NAME="@default1315"></A><FONT COLOR=black><FONT SIZE=3>
| | </PRE> |
| </FONT></FONT><A NAME="@default1316"></A><FONT COLOR=black><FONT SIZE=3>
| | |
| </FONT></FONT><A NAME="@default1317"></A></P><P><FONT COLOR=black><FONT SIZE=3>The built-in function </FONT></FONT><FONT COLOR=black><FONT SIZE=3><TT>repr</TT></FONT></FONT><FONT COLOR=black><FONT SIZE=3> can help. It takes any object as an
| | |
| | |
| | |
| | The built-in function <TT>repr</TT> can help. It takes any object as an |
| argument and returns a string representation of the object. For | | argument and returns a string representation of the object. For |
| strings, it represents whitespace | | strings, it represents whitespace |
| characters with backslash sequences:</FONT></FONT></P><PRE CLASS="verbatim"><FONT COLOR=blue><FONT SIZE=4>>>> print repr(s) | | characters with backslash sequences: |
| | <PRE CLASS="verbatim">>>> print repr(s) |
| '1 2\t 3\n 4' | | '1 2\t 3\n 4' |
| </FONT></FONT></PRE><P><FONT COLOR=black><FONT SIZE=3>This can be helpful for debugging.</FONT></FONT></P><P><FONT COLOR=black><FONT SIZE=3>One other problem you might run into is that different systems
| | </PRE> |
| | This can be helpful for debugging. |
| | |
| | One other problem you might run into is that different systems |
| use different characters to indicate the end of a line. Some | | use different characters to indicate the end of a line. Some |
| systems use a newline, represented </FONT></FONT><CODE><FONT COLOR=black><FONT SIZE=3>\n</FONT></FONT></CODE><FONT COLOR=black><FONT SIZE=3>. Others use | | systems use a newline, represented <CODE>\n</CODE>. Others use |
| a return character, represented </FONT></FONT><CODE><FONT COLOR=black><FONT SIZE=3>\r</FONT></FONT></CODE><FONT COLOR=black><FONT SIZE=3>. Some use both. | | a return character, represented <CODE>\r</CODE>. Some use both. |
| If you move files between different systems, these inconsistencies | | If you move files between different systems, these inconsistencies |
| might cause problems.</FONT></FONT></P><P><A NAME="@default1318"></A></P><P><FONT COLOR=black><FONT SIZE=3>For most systems, there are applications to convert from one | | might cause problems. |
| | |
| | For most systems, there are applications to convert from one |
| format to another. You can find them (and read more about this | | format to another. You can find them (and read more about this |
| issue) at </FONT></FONT><FONT COLOR=black><FONT SIZE=3><TT>wikipedia.org/wiki/Newline</TT></FONT></FONT><FONT COLOR=black><FONT SIZE=3>. Or, of course, you | | issue) at <TT>wikipedia.org/wiki/Newline</TT>. Or, of course, you |
| could write one yourself.</FONT></FONT></P><H2 CLASS="section"><A NAME="toc163"></A><A NAME="htoc179"><FONT COLOR=black><FONT SIZE=3>14.11</FONT></FONT></A><FONT COLOR=black><FONT SIZE=3>  Glossary</FONT></FONT></H2><DL CLASS="description"><DT CLASS="dt-description"><FONT COLOR=black><FONT SIZE=3><B>persistent:</B></FONT></FONT></DT><DD CLASS="dd-description"><FONT COLOR=black><FONT SIZE=3> Pertaining to a program that runs indefinitely | | could write one yourself. |
| | === 14.11  Glossary === |
| | |
| | <DL CLASS="description"><DT CLASS="dt-description">'''persistent:'''</DT><DD CLASS="dd-description"> Pertaining to a program that runs indefinitely |
| and keeps at least some of its data in permanent storage. | | and keeps at least some of its data in permanent storage. |
| </FONT></FONT><A NAME="@default1319"></A></DD><DT CLASS="dt-description"><FONT COLOR=black><FONT SIZE=3><B>format operator:</B></FONT></FONT></DT><DD CLASS="dd-description"><FONT COLOR=black><FONT SIZE=3> An operator, </FONT></FONT><FONT COLOR=black><FONT SIZE=3><TT>%</TT></FONT></FONT><FONT COLOR=black><FONT SIZE=3>, that takes a format
| | </DD><DT CLASS="dt-description">'''format operator:'''</DT><DD CLASS="dd-description"> An operator, <TT>%</TT>, that takes a format |
| string and a tuple and generates a string that includes | | string and a tuple and generates a string that includes |
| the elements of the tuple formatted as specified by the format string. | | the elements of the tuple formatted as specified by the format string. |
| </FONT></FONT><A NAME="@default1320"></A><FONT COLOR=black><FONT SIZE=3>
| | |
| </FONT></FONT><A NAME="@default1321"></A></DD><DT CLASS="dt-description"><FONT COLOR=black><FONT SIZE=3><B>format string:</B></FONT></FONT></DT><DD CLASS="dd-description"><FONT COLOR=black><FONT SIZE=3> A string, used with the format operator, that
| | </DD><DT CLASS="dt-description">'''format string:'''</DT><DD CLASS="dd-description"> A string, used with the format operator, that |
| contains format sequences. | | contains format sequences. |
| </FONT></FONT><A NAME="@default1322"></A></DD><DT CLASS="dt-description"><FONT COLOR=black><FONT SIZE=3><B>format sequence:</B></FONT></FONT></DT><DD CLASS="dd-description"><FONT COLOR=black><FONT SIZE=3> A sequence of characters in a format string,
| | </DD><DT CLASS="dt-description">'''format sequence:'''</DT><DD CLASS="dd-description"> A sequence of characters in a format string, |
| like </FONT></FONT><FONT COLOR=black><FONT SIZE=3><TT>%d</TT></FONT></FONT><FONT COLOR=black><FONT SIZE=3>, that specifies how a value should be formatted. | | like <TT>%d</TT>, that specifies how a value should be formatted. |
| </FONT></FONT><A NAME="@default1323"></A></DD><DT CLASS="dt-description"><FONT COLOR=black><FONT SIZE=3><B>text file:</B></FONT></FONT></DT><DD CLASS="dd-description"><FONT COLOR=black><FONT SIZE=3> A sequence of characters stored in permanent
| | </DD><DT CLASS="dt-description">'''text file:'''</DT><DD CLASS="dd-description"> A sequence of characters stored in permanent |
| storage like a hard drive. | | storage like a hard drive. |
| </FONT></FONT><A NAME="@default1324"></A></DD><DT CLASS="dt-description"><FONT COLOR=black><FONT SIZE=3><B>directory:</B></FONT></FONT></DT><DD CLASS="dd-description"><FONT COLOR=black><FONT SIZE=3> A named collection of files, also called a folder.
| | </DD><DT CLASS="dt-description">'''directory:'''</DT><DD CLASS="dd-description"> A named collection of files, also called a folder. |
| </FONT></FONT><A NAME="@default1325"></A></DD><DT CLASS="dt-description"><FONT COLOR=black><FONT SIZE=3><B>path:</B></FONT></FONT></DT><DD CLASS="dd-description"><FONT COLOR=black><FONT SIZE=3> A string that identifies a file.
| | </DD><DT CLASS="dt-description">'''path:'''</DT><DD CLASS="dd-description"> A string that identifies a file. |
| </FONT></FONT><A NAME="@default1326"></A></DD><DT CLASS="dt-description"><FONT COLOR=black><FONT SIZE=3><B>relative path:</B></FONT></FONT></DT><DD CLASS="dd-description"><FONT COLOR=black><FONT SIZE=3> A path that starts from the current directory.
| | </DD><DT CLASS="dt-description">'''relative path:'''</DT><DD CLASS="dd-description"> A path that starts from the current directory. |
| </FONT></FONT><A NAME="@default1327"></A></DD><DT CLASS="dt-description"><FONT COLOR=black><FONT SIZE=3><B>absolute path:</B></FONT></FONT></DT><DD CLASS="dd-description"><FONT COLOR=black><FONT SIZE=3> A path that starts from the topmost directory
| | </DD><DT CLASS="dt-description">'''absolute path:'''</DT><DD CLASS="dd-description"> A path that starts from the topmost directory |
| in the file system. | | in the file system. |
| </FONT></FONT><A NAME="@default1328"></A></DD><DT CLASS="dt-description"><FONT COLOR=black><FONT SIZE=3><B>catch:</B></FONT></FONT></DT><DD CLASS="dd-description"><FONT COLOR=black><FONT SIZE=3> To prevent an exception from terminating
| | </DD><DT CLASS="dt-description">'''catch:'''</DT><DD CLASS="dd-description"> To prevent an exception from terminating |
| a program using the </FONT></FONT><FONT COLOR=black><FONT SIZE=3><TT>try</TT></FONT></FONT><FONT COLOR=black><FONT SIZE=3> | | a program using the <TT>try</TT> |
| and </FONT></FONT><FONT COLOR=black><FONT SIZE=3><TT>except</TT></FONT></FONT><FONT COLOR=black><FONT SIZE=3> statements. | | and <TT>except</TT> statements. |
| </FONT></FONT><A NAME="@default1329"></A></DD><DT CLASS="dt-description"><FONT COLOR=black><FONT SIZE=3><B>database:</B></FONT></FONT></DT><DD CLASS="dd-description"><FONT COLOR=black><FONT SIZE=3> A file whose contents are organized like a dictionary
| | </DD><DT CLASS="dt-description">'''database:'''</DT><DD CLASS="dd-description"> A file whose contents are organized like a dictionary |
| with keys that correspond to values. | | with keys that correspond to values. |
| </FONT></FONT><A NAME="@default1330"></A></DD></DL><H2 CLASS="section"><A NAME="toc164"></A><A NAME="htoc180"><FONT COLOR=black><FONT SIZE=3>14.12</FONT></FONT></A><FONT COLOR=black><FONT SIZE=3>  Exercises</FONT></FONT></H2><DIV CLASS="theorem"><FONT COLOR=black><FONT SIZE=3><B>Exercise 5</B></FONT></FONT><FONT COLOR=black><FONT SIZE=3>  <EM>
| | </DD></DL>=== 14.12  Exercises === |
| </EM></FONT></FONT><A NAME="urllib"></A><P><A NAME="@default1331"></A><FONT COLOR=black><FONT SIZE=3><EM>
| | |
| </EM></FONT></FONT><A NAME="@default1332"></A><FONT COLOR=black><FONT SIZE=3><EM>
| | <DIV CLASS="theorem">'''Exercise 5'''  '' |
| </EM></FONT></FONT><A NAME="@default1333"></A></P><P><FONT COLOR=black><FONT SIZE=3><EM>The </EM></FONT></FONT><FONT COLOR=black><FONT SIZE=3><EM><TT>urllib</TT></EM></FONT></FONT><FONT COLOR=black><FONT SIZE=3><EM> module provides methods for manipulating URLs
| | '' |
| | '' |
| | '''' |
| | '' |
| | |
| | ''The ''''<TT>urllib</TT>'''' module provides methods for manipulating URLs |
| and downloading information from the web. The following example | | and downloading information from the web. The following example |
| downloads and prints a secret message from </EM></FONT></FONT><FONT COLOR=black><FONT SIZE=3><EM><TT>thinkpython.com</TT></EM></FONT></FONT><FONT COLOR=black><FONT SIZE=3><EM>:</EM></FONT></FONT></P><PRE CLASS="verbatim"><EM><FONT COLOR=blue><FONT SIZE=4>import urllib | | downloads and prints a secret message from ''''<TT>thinkpython.com</TT>'''':'' |
| | <PRE CLASS="verbatim">''import urllib |
|
| |
|
| conn = urllib.urlopen('http://thinkpython.com/secret.html') | | conn = urllib.urlopen('http://thinkpython.com/secret.html') |
| for line in conn.fp: | | for line in conn.fp: |
| print line.strip() | | print line.strip() |
| </FONT></FONT></EM></PRE><P><EM><FONT COLOR=black><FONT SIZE=3>Run this code and follow the instructions you see there.</FONT></FONT></EM></P><P><A NAME="@default1334"></A><EM><FONT COLOR=black><FONT SIZE=3>
| | ''</PRE> |
| </FONT></FONT></EM><A NAME="@default1335"></A></P></DIV><DIV CLASS="theorem"><FONT COLOR=black><FONT SIZE=3><B>Exercise 6</B></FONT></FONT><FONT COLOR=black><FONT SIZE=3>  <EM>
| | ''Run this code and follow the instructions you see there.'' |
| </EM></FONT></FONT><A NAME="checksum"></A><P><A NAME="@default1336"></A></P><P><FONT COLOR=black><FONT SIZE=3><EM>In a large collection of MP3 files, there may be more than one
| | |
| | '' |
| | '' |
| | </DIV><DIV CLASS="theorem">'''Exercise 6'''  '' |
| | '' |
| | |
| | ''In a large collection of MP3 files, there may be more than one |
| copy of the same song, stored in different directories or with | | copy of the same song, stored in different directories or with |
| different file names. The goal of this exercise is to search for | | different file names. The goal of this exercise is to search for |
| these duplicates.</EM></FONT></FONT></P><OL CLASS="enumerate" type=1><LI CLASS="li-enumerate"><FONT COLOR=black><FONT SIZE=3><EM>Write a program that searches a directory and all of its | | these duplicates.'' |
| | |
| | *''Write a program that searches a directory and all of its |
| subdirectories, recursively, and returns a list of complete paths | | subdirectories, recursively, and returns a list of complete paths |
| for all files with a given suffix (like </EM></FONT></FONT><FONT COLOR=black><FONT SIZE=3><EM><TT>.mp3</TT></EM></FONT></FONT><FONT COLOR=black><FONT SIZE=3><EM>). | | for all files with a given suffix (like ''''<TT>.mp3</TT>''''). |
| Hint: </EM></FONT></FONT><FONT COLOR=black><FONT SIZE=3><EM><TT>os.path</TT></EM></FONT></FONT><FONT COLOR=black><FONT SIZE=3><EM> provides several useful functions for | | Hint: ''''<TT>os.path</TT>'''' provides several useful functions for |
| manipulating file and path names.</EM></FONT></FONT><P><A NAME="@default1337"></A><FONT COLOR=black><FONT SIZE=3><EM> | | manipulating file and path names.'' |
| </EM></FONT></FONT><A NAME="@default1338"></A><FONT COLOR=black><FONT SIZE=3><EM>
| | '' |
| </EM></FONT></FONT><A NAME="@default1339"></A><FONT COLOR=black><FONT SIZE=3><EM>
| | '''' |
| </EM></FONT></FONT><A NAME="@default1340"></A></P></LI><LI CLASS="li-enumerate"><FONT COLOR=black><FONT SIZE=3><EM>To recognize duplicates, you can use a hash function that
| | '''' |
| | '' |
| | |
| | *''To recognize duplicates, you can use a hash function that |
| reads the file and generates a short summary | | reads the file and generates a short summary |
| of the contents. For example, | | of the contents. For example, |
| Line 385: |
Line 636: |
| “message” and returns a 128-bit “checksum.” The probability | | “message” and returns a 128-bit “checksum.” The probability |
| is very small that two files with different contents will | | is very small that two files with different contents will |
| return the same checksum.</EM></FONT></FONT><P><FONT COLOR=black><FONT SIZE=3><EM>You can read about MD5 at </EM></FONT></FONT><FONT COLOR=black><FONT SIZE=3><EM><TT>wikipedia.org/wiki/Md5</TT></EM></FONT></FONT><FONT COLOR=black><FONT SIZE=3><EM>. On | | return the same checksum.'' |
| a Unix system you can use the program </EM></FONT></FONT><FONT COLOR=black><FONT SIZE=3><EM><TT>md5sum</TT></EM></FONT></FONT><FONT COLOR=black><FONT SIZE=3><EM> and a pipe to | | ''You can read about MD5 at ''''<TT>wikipedia.org/wiki/Md5</TT>''''. On |
| compute checksums from Python.</EM></FONT></FONT></P><P><A NAME="@default1341"></A></P></LI></OL></DIV><DIV CLASS="theorem"><FONT COLOR=black><FONT SIZE=3><B>Exercise 7</B></FONT></FONT><FONT COLOR=black><FONT SIZE=3>  </FONT></FONT><P><A NAME="@default1342"></A><FONT COLOR=black><FONT SIZE=3><EM> | | a Unix system you can use the program ''''<TT>md5sum</TT>'''' and a pipe to |
| </EM></FONT></FONT><A NAME="@default1343"></A><FONT COLOR=black><FONT SIZE=3><EM>
| | compute checksums from Python.'' |
| </EM></FONT></FONT><A NAME="@default1344"></A></P><P><FONT COLOR=black><FONT SIZE=3><EM>The Internet Movie Database (IMDb) is an online collection of
| | |
| | </DIV><DIV CLASS="theorem">'''Exercise 7'''   |
| | '' |
| | '''' |
| | '' |
| | |
| | ''The Internet Movie Database (IMDb) is an online collection of |
| information about movies. Their database is available | | information about movies. Their database is available |
| in plain text format, so it is reasonably easy to read from | | in plain text format, so it is reasonably easy to read from |
| Python. For this exercise, the files you need | | Python. For this exercise, the files you need |
| are </EM></FONT></FONT><FONT COLOR=black><FONT SIZE=3><EM><TT>actors.list.gz</TT></EM></FONT></FONT><FONT COLOR=black><FONT SIZE=3><EM> and </EM></FONT></FONT><FONT COLOR=black><FONT SIZE=3><EM><TT>actresses.list.gz</TT></EM></FONT></FONT><FONT COLOR=black><FONT SIZE=3><EM>; you | | are ''''<TT>actors.list.gz</TT>'''' and ''''<TT>actresses.list.gz</TT>''''; you |
| can download them from </EM></FONT></FONT><FONT COLOR=black><FONT SIZE=3><EM><TT>www.imdb.com/interfaces#plain</TT></EM></FONT></FONT><FONT COLOR=black><FONT SIZE=3><EM>.</EM></FONT></FONT></P><P><A NAME="@default1345"></A><FONT COLOR=black><FONT SIZE=3><EM> | | can download them from ''''<TT>www.imdb.com/interfaces#plain</TT>''''.'' |
| </EM></FONT></FONT><A NAME="@default1346"></A><FONT COLOR=black><FONT SIZE=3><EM>
| | |
| </EM></FONT></FONT><A NAME="@default1347"></A></P><P><FONT COLOR=black><FONT SIZE=3><EM>I have written a program that parses these files and
| | '' |
| | '''' |
| | '' |
| | |
| | ''I have written a program that parses these files and |
| splits them into actor names, movie titles, etc. You can | | splits them into actor names, movie titles, etc. You can |
| download it from </EM></FONT></FONT><FONT COLOR=black><FONT SIZE=3><EM><TT>thinkpython.com/code/imdb.py</TT></EM></FONT></FONT><FONT COLOR=black><FONT SIZE=3><EM>.</EM></FONT></FONT></P><P><FONT COLOR=black><FONT SIZE=3><EM>If you run </EM></FONT></FONT><FONT COLOR=black><FONT SIZE=3><EM><TT>imdb.py</TT></EM></FONT></FONT><FONT COLOR=black><FONT SIZE=3><EM> as a script, it reads </EM></FONT></FONT><FONT COLOR=black><FONT SIZE=3><EM><TT>actors.list.gz</TT></EM></FONT></FONT><FONT COLOR=black><FONT SIZE=3><EM> | | download it from ''''<TT>thinkpython.com/code/imdb.py</TT>''''.'' |
| and prints one actor-movie pair per line. Or, if you </EM></FONT></FONT><FONT COLOR=black><FONT SIZE=3><EM><TT>import | | |
| imdb</TT></EM></FONT></FONT><FONT COLOR=black><FONT SIZE=3><EM> you can use the function </EM></FONT></FONT><CODE><FONT COLOR=black><FONT SIZE=3><EM>process_file</EM></FONT></FONT></CODE><FONT COLOR=black><FONT SIZE=3><EM> to, well, | | ''If you run ''''<TT>imdb.py</TT>'''' as a script, it reads ''''<TT>actors.list.gz</TT>'''' |
| | and prints one actor-movie pair per line. Or, if you ''''<TT>import |
| | imdb</TT>'''' you can use the function ''<CODE>''process_file''</CODE>'' to, well, |
| process the file. The arguments are a filename, a function | | process the file. The arguments are a filename, a function |
| object and an optional number of lines to process. Here is | | object and an optional number of lines to process. Here is |
| an example:</EM></FONT></FONT></P><PRE CLASS="verbatim"><EM><FONT COLOR=blue><FONT SIZE=4>import imdb | | an example:'' |
| | <PRE CLASS="verbatim">''import imdb |
|
| |
|
| def print_info(actor, date, title, role): | | def print_info(actor, date, title, role): |
| Line 409: |
Line 673: |
|
| |
|
| imdb.process_file('actors.list.gz', print_info) | | imdb.process_file('actors.list.gz', print_info) |
| </FONT></FONT></EM></PRE><P><EM><FONT COLOR=black><FONT SIZE=3>When you call </FONT></FONT></EM><CODE><EM><FONT COLOR=black><FONT SIZE=3>process_file</FONT></FONT></EM></CODE><EM><FONT COLOR=black><FONT SIZE=3>, it opens </FONT></FONT></EM><EM><FONT COLOR=black><FONT SIZE=3><TT>filename</TT></FONT></FONT></EM><EM><FONT COLOR=black><FONT SIZE=3>, reads the
| | ''</PRE> |
| contents, and calls </FONT></FONT></EM><CODE><EM><FONT COLOR=black><FONT SIZE=3>print_info</FONT></FONT></EM></CODE><EM><FONT COLOR=black><FONT SIZE=3> once for each line in the file. | | ''When you call ''<CODE>''process_file''</CODE>'', it opens ''''<TT>filename</TT>'''', reads the |
| </FONT></FONT></EM><CODE><EM><FONT COLOR=black><FONT SIZE=3>print_info</FONT></FONT></EM></CODE><EM><FONT COLOR=black><FONT SIZE=3> takes an actor, date, movie title and role as
| | contents, and calls ''<CODE>''print_info''</CODE>'' once for each line in the file. |
| arguments and prints them.</FONT></FONT></EM></P><OL CLASS="enumerate" type=1><LI CLASS="li-enumerate"><EM><FONT COLOR=black><FONT SIZE=3>Write a program that reads </FONT></FONT></EM><EM><FONT COLOR=black><FONT SIZE=3><TT>actors.list.gz</TT></FONT></FONT></EM><EM><FONT COLOR=black><FONT SIZE=3> and </FONT></FONT></EM><EM><FONT COLOR=black><FONT SIZE=3><TT>actresses.list.gz</TT></FONT></FONT></EM><EM><FONT COLOR=black><FONT SIZE=3> and uses </FONT></FONT></EM><EM><FONT COLOR=black><FONT SIZE=3><TT>shelve</TT></FONT></FONT></EM><EM><FONT COLOR=black><FONT SIZE=3> to build a database | | ''<CODE>''print_info''</CODE>'' takes an actor, date, movie title and role as |
| that maps from each actor to a list of his or her films.</FONT></FONT></EM><P><A NAME="@default1348"></A><EM><FONT COLOR=black><FONT SIZE=3> | | arguments and prints them.'' |
| </FONT></FONT></EM><A NAME="@default1349"></A></P></LI><LI CLASS="li-enumerate"><EM><FONT COLOR=black><FONT SIZE=3>Two actors are “costars” if they have been in at least one
| | |
| | *''Write a program that reads ''''<TT>actors.list.gz</TT>'''' and ''''<TT>actresses.list.gz</TT>'''' and uses ''''<TT>shelve</TT>'''' to build a database |
| | that maps from each actor to a list of his or her films.'' |
| | '' |
| | '' |
| | |
| | *''Two actors are “costars” if they have been in at least one |
| movie together. Process the database you built in the previous step | | movie together. Process the database you built in the previous step |
| and build a second database that maps from each actor to a list of | | and build a second database that maps from each actor to a list of |
| his or her costars.</FONT></FONT></EM><P><A NAME="@default1350"></A><EM><FONT COLOR=black><FONT SIZE=3> | | his or her costars.'' |
| </FONT></FONT></EM><A NAME="@default1351"></A></P></LI><LI CLASS="li-enumerate"><EM><FONT COLOR=black><FONT SIZE=3>Write a program that can play the “Six Degrees of Kevin
| | '' |
| | '' |
| | |
| | *''Write a program that can play the “Six Degrees of Kevin |
| Bacon,” which you can read about at | | Bacon,” which you can read about at |
| </FONT></FONT></EM><EM><FONT COLOR=black><FONT SIZE=3><TT>wikipedia.org/wiki/Six_Degrees_of_Kevin_Bacon</TT></FONT></FONT></EM><EM><FONT COLOR=black><FONT SIZE=3>. This
| | ''''<TT>wikipedia.org/wiki/Six_Degrees_of_Kevin_Bacon</TT>''''. This |
| problem is challenging because it requires you to find the shortest | | problem is challenging because it requires you to find the shortest |
| path in a graph. You can read about shortest path algorithms | | path in a graph. You can read about shortest path algorithms |
| at </FONT></FONT></EM><EM><FONT COLOR=black><FONT SIZE=3><TT>wikipedia.org/wiki/Shortest_path_problem</TT></FONT></FONT></EM><EM><FONT COLOR=black><FONT SIZE=3>.</FONT></FONT></EM></LI></OL></DIV><HR> | | at ''''<TT>wikipedia.org/wiki/Shortest_path_problem</TT>''''.'' |
| <A HREF="book014.html"><IMG SRC="previous_motif.gif" ALT="Previous"></A>
| | |
| <A HREF="index.html"><IMG SRC="contents_motif.gif" ALT="Up"></A>
| | </DIV><HR> |
| <A HREF="book016.html"><IMG SRC="next_motif.gif" ALT="Next"></A>
| | <IMG SRC="previous_motif.gif" ALT="Previous"> |
| </BODY>
| | <IMG SRC="contents_motif.gif" ALT="Up"> |
| </HTML>
| | <IMG SRC="next_motif.gif" ALT="Next"> |