| 1 | <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN"> |
| 2 | <html> |
| 3 | <head> |
| 4 | <link rel="STYLESHEET" href="lib.css" type='text/css' /> |
| 5 | <link rel="SHORTCUT ICON" href="../icons/pyfav.png" type="image/png" /> |
| 6 | <link rel='start' href='../index.html' title='Python Documentation Index' /> |
| 7 | <link rel="first" href="lib.html" title='Python Library Reference' /> |
| 8 | <link rel='contents' href='contents.html' title="Contents" /> |
| 9 | <link rel='index' href='genindex.html' title='Index' /> |
| 10 | <link rel='last' href='about.html' title='About this document...' /> |
| 11 | <link rel='help' href='about.html' title='About this document...' /> |
| 12 | <link rel="next" href="module-pickle.html" /> |
| 13 | <link rel="prev" href="module-traceback.html" /> |
| 14 | <link rel="parent" href="python.html" /> |
| 15 | <link rel="next" href="module-pickle.html" /> |
| 16 | <meta name='aesop' content='information' /> |
| 17 | <title>3.13 linecache -- Random access to text lines</title> |
| 18 | </head> |
| 19 | <body> |
| 20 | <DIV CLASS="navigation"> |
| 21 | <div id='top-navigation-panel' xml:id='top-navigation-panel'> |
| 22 | <table align="center" width="100%" cellpadding="0" cellspacing="2"> |
| 23 | <tr> |
| 24 | <td class='online-navigation'><a rel="prev" title="3.12.1 Traceback Example" |
| 25 | href="traceback-example.html"><img src='../icons/previous.png' |
| 26 | border='0' height='32' alt='Previous Page' width='32' /></A></td> |
| 27 | <td class='online-navigation'><a rel="parent" title="3. Python Runtime Services" |
| 28 | href="python.html"><img src='../icons/up.png' |
| 29 | border='0' height='32' alt='Up One Level' width='32' /></A></td> |
| 30 | <td class='online-navigation'><a rel="next" title="3.14 pickle " |
| 31 | href="module-pickle.html"><img src='../icons/next.png' |
| 32 | border='0' height='32' alt='Next Page' width='32' /></A></td> |
| 33 | <td align="center" width="100%">Python Library Reference</td> |
| 34 | <td class='online-navigation'><a rel="contents" title="Table of Contents" |
| 35 | href="contents.html"><img src='../icons/contents.png' |
| 36 | border='0' height='32' alt='Contents' width='32' /></A></td> |
| 37 | <td class='online-navigation'><a href="modindex.html" title="Module Index"><img src='../icons/modules.png' |
| 38 | border='0' height='32' alt='Module Index' width='32' /></a></td> |
| 39 | <td class='online-navigation'><a rel="index" title="Index" |
| 40 | href="genindex.html"><img src='../icons/index.png' |
| 41 | border='0' height='32' alt='Index' width='32' /></A></td> |
| 42 | </tr></table> |
| 43 | <div class='online-navigation'> |
| 44 | <b class="navlabel">Previous:</b> |
| 45 | <a class="sectref" rel="prev" href="traceback-example.html">3.12.1 Traceback Example</A> |
| 46 | <b class="navlabel">Up:</b> |
| 47 | <a class="sectref" rel="parent" href="python.html">3. Python Runtime Services</A> |
| 48 | <b class="navlabel">Next:</b> |
| 49 | <a class="sectref" rel="next" href="module-pickle.html">3.14 pickle </A> |
| 50 | </div> |
| 51 | <hr /></div> |
| 52 | </DIV> |
| 53 | <!--End of Navigation Panel--> |
| 54 | |
| 55 | <H1><A NAME="SECTION0051300000000000000000"> |
| 56 | 3.13 <tt class="module">linecache</tt> -- |
| 57 | Random access to text lines</A> |
| 58 | </H1> |
| 59 | |
| 60 | <P> |
| 61 | <A NAME="module-linecache"></A> |
| 62 | |
| 63 | <P> |
| 64 | The <tt class="module">linecache</tt> module allows one to get any line from any file, |
| 65 | while attempting to optimize internally, using a cache, the common case |
| 66 | where many lines are read from a single file. This is used by the |
| 67 | <tt class="module"><a href="module-traceback.html">traceback</a></tt> module to retrieve source lines for inclusion in |
| 68 | the formatted traceback. |
| 69 | |
| 70 | <P> |
| 71 | The <tt class="module">linecache</tt> module defines the following functions: |
| 72 | |
| 73 | <P> |
| 74 | <dl><dt><table cellpadding="0" cellspacing="0"><tr valign="baseline"> |
| 75 | <td><nobr><b><tt id='l2h-621' xml:id='l2h-621' class="function">getline</tt></b>(</nobr></td> |
| 76 | <td><var>filename, lineno</var>)</td></tr></table></dt> |
| 77 | <dd> |
| 78 | Get line <var>lineno</var> from file named <var>filename</var>. This function |
| 79 | will never throw an exception -- it will return <code>''</code> on errors |
| 80 | (the terminating newline character will be included for lines that are |
| 81 | found). |
| 82 | |
| 83 | <P> |
| 84 | If a file named <var>filename</var> is not found, the function will look |
| 85 | for it in the module<a id='l2h-622' xml:id='l2h-622'></a> search path, |
| 86 | <code>sys.path</code>. |
| 87 | </dl> |
| 88 | |
| 89 | <P> |
| 90 | <dl><dt><table cellpadding="0" cellspacing="0"><tr valign="baseline"> |
| 91 | <td><nobr><b><tt id='l2h-623' xml:id='l2h-623' class="function">clearcache</tt></b>(</nobr></td> |
| 92 | <td><var></var>)</td></tr></table></dt> |
| 93 | <dd> |
| 94 | Clear the cache. Use this function if you no longer need lines from |
| 95 | files previously read using <tt class="function">getline()</tt>. |
| 96 | </dl> |
| 97 | |
| 98 | <P> |
| 99 | <dl><dt><table cellpadding="0" cellspacing="0"><tr valign="baseline"> |
| 100 | <td><nobr><b><tt id='l2h-624' xml:id='l2h-624' class="function">checkcache</tt></b>(</nobr></td> |
| 101 | <td><var></var><big>[</big><var>filename</var><big>]</big><var></var>)</td></tr></table></dt> |
| 102 | <dd> |
| 103 | Check the cache for validity. Use this function if files in the cache |
| 104 | may have changed on disk, and you require the updated version. If |
| 105 | <var>filename</var> is omitted, it will check the whole cache entries. |
| 106 | </dl> |
| 107 | |
| 108 | <P> |
| 109 | Example: |
| 110 | |
| 111 | <P> |
| 112 | <div class="verbatim"><pre> |
| 113 | >>> import linecache |
| 114 | >>> linecache.getline('/etc/passwd', 4) |
| 115 | 'sys:x:3:3:sys:/dev:/bin/sh\n' |
| 116 | </pre></div> |
| 117 | |
| 118 | <DIV CLASS="navigation"> |
| 119 | <div class='online-navigation'> |
| 120 | <p></p><hr /> |
| 121 | <table align="center" width="100%" cellpadding="0" cellspacing="2"> |
| 122 | <tr> |
| 123 | <td class='online-navigation'><a rel="prev" title="3.12.1 Traceback Example" |
| 124 | href="traceback-example.html"><img src='../icons/previous.png' |
| 125 | border='0' height='32' alt='Previous Page' width='32' /></A></td> |
| 126 | <td class='online-navigation'><a rel="parent" title="3. Python Runtime Services" |
| 127 | href="python.html"><img src='../icons/up.png' |
| 128 | border='0' height='32' alt='Up One Level' width='32' /></A></td> |
| 129 | <td class='online-navigation'><a rel="next" title="3.14 pickle " |
| 130 | href="module-pickle.html"><img src='../icons/next.png' |
| 131 | border='0' height='32' alt='Next Page' width='32' /></A></td> |
| 132 | <td align="center" width="100%">Python Library Reference</td> |
| 133 | <td class='online-navigation'><a rel="contents" title="Table of Contents" |
| 134 | href="contents.html"><img src='../icons/contents.png' |
| 135 | border='0' height='32' alt='Contents' width='32' /></A></td> |
| 136 | <td class='online-navigation'><a href="modindex.html" title="Module Index"><img src='../icons/modules.png' |
| 137 | border='0' height='32' alt='Module Index' width='32' /></a></td> |
| 138 | <td class='online-navigation'><a rel="index" title="Index" |
| 139 | href="genindex.html"><img src='../icons/index.png' |
| 140 | border='0' height='32' alt='Index' width='32' /></A></td> |
| 141 | </tr></table> |
| 142 | <div class='online-navigation'> |
| 143 | <b class="navlabel">Previous:</b> |
| 144 | <a class="sectref" rel="prev" href="traceback-example.html">3.12.1 Traceback Example</A> |
| 145 | <b class="navlabel">Up:</b> |
| 146 | <a class="sectref" rel="parent" href="python.html">3. Python Runtime Services</A> |
| 147 | <b class="navlabel">Next:</b> |
| 148 | <a class="sectref" rel="next" href="module-pickle.html">3.14 pickle </A> |
| 149 | </div> |
| 150 | </div> |
| 151 | <hr /> |
| 152 | <span class="release-info">Release 2.4.2, documentation updated on 28 September 2005.</span> |
| 153 | </DIV> |
| 154 | <!--End of Navigation Panel--> |
| 155 | <ADDRESS> |
| 156 | See <i><a href="about.html">About this document...</a></i> for information on suggesting changes. |
| 157 | </ADDRESS> |
| 158 | </BODY> |
| 159 | </HTML> |