<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <title>11.9. linecache — Random access to text lines — Python v2.6.5 documentation</title> <link rel="stylesheet" href="../_static/default.css" type="text/css" /> <link rel="stylesheet" href="../_static/pygments.css" type="text/css" /> <script type="text/javascript"> var DOCUMENTATION_OPTIONS = { URL_ROOT: '../', VERSION: '2.6.5', COLLAPSE_MODINDEX: false, FILE_SUFFIX: '.html', HAS_SOURCE: true }; </script> <script type="text/javascript" src="../_static/jquery.js"></script> <script type="text/javascript" src="../_static/doctools.js"></script> <link rel="search" type="application/opensearchdescription+xml" title="Search within Python v2.6.5 documentation" href="../_static/opensearch.xml"/> <link rel="author" title="About these documents" href="../about.html" /> <link rel="copyright" title="Copyright" href="../copyright.html" /> <link rel="top" title="Python v2.6.5 documentation" href="../index.html" /> <link rel="up" title="11. File and Directory Access" href="filesys.html" /> <link rel="next" title="11.10. shutil — High-level file operations" href="shutil.html" /> <link rel="prev" title="11.8. fnmatch — Unix filename pattern matching" href="fnmatch.html" /> <link rel="shortcut icon" type="image/png" href="../_static/py.png" /> </head> <body> <div class="related"> <h3>Navigation</h3> <ul> <li class="right" style="margin-right: 10px"> <a href="../genindex.html" title="General Index" accesskey="I">index</a></li> <li class="right" > <a href="../modindex.html" title="Global Module Index" accesskey="M">modules</a> |</li> <li class="right" > <a href="shutil.html" title="11.10. shutil — High-level file operations" accesskey="N">next</a> |</li> <li class="right" > <a href="fnmatch.html" title="11.8. fnmatch — Unix filename pattern matching" accesskey="P">previous</a> |</li> <li><img src="../_static/py.png" alt="" style="vertical-align: middle; margin-top: -1px"/></li> <li><a href="../index.html">Python v2.6.5 documentation</a> »</li> <li><a href="index.html" >The Python Standard Library</a> »</li> <li><a href="filesys.html" accesskey="U">11. File and Directory Access</a> »</li> </ul> </div> <div class="document"> <div class="documentwrapper"> <div class="bodywrapper"> <div class="body"> <div class="section" id="module-linecache"> <h1>11.9. <tt class="xref docutils literal"><span class="pre">linecache</span></tt> — Random access to text lines<a class="headerlink" href="#module-linecache" title="Permalink to this headline">¶</a></h1> <p>The <tt class="xref docutils literal"><span class="pre">linecache</span></tt> module allows one to get any line from any file, while attempting to optimize internally, using a cache, the common case where many lines are read from a single file. This is used by the <a title="Print or retrieve a stack traceback." class="reference external" href="traceback.html#module-traceback"><tt class="xref docutils literal"><span class="pre">traceback</span></tt></a> module to retrieve source lines for inclusion in the formatted traceback.</p> <p>The <tt class="xref docutils literal"><span class="pre">linecache</span></tt> module defines the following functions:</p> <dl class="function"> <dt id="linecache.getline"> <tt class="descclassname">linecache.</tt><tt class="descname">getline</tt><big>(</big><em>filename</em>, <em>lineno</em><span class="optional">[</span>, <em>module_globals</em><span class="optional">]</span><big>)</big><a class="headerlink" href="#linecache.getline" title="Permalink to this definition">¶</a></dt> <dd><p>Get line <em>lineno</em> from file named <em>filename</em>. This function will never throw an exception — it will return <tt class="docutils literal"><span class="pre">''</span></tt> on errors (the terminating newline character will be included for lines that are found).</p> <p id="index-410">If a file named <em>filename</em> is not found, the function will look for it in the module search path, <tt class="docutils literal"><span class="pre">sys.path</span></tt>, after first checking for a <span class="target" id="index-411"></span><a class="reference external" href="http://www.python.org/dev/peps/pep-0302"><strong>PEP 302</strong></a> <tt class="docutils literal"><span class="pre">__loader__</span></tt> in <em>module_globals</em>, in case the module was imported from a zipfile or other non-filesystem import source.</p> <p class="versionadded"> <span class="versionmodified">New in version 2.5: </span>The <em>module_globals</em> parameter was added.</p> </dd></dl> <dl class="function"> <dt id="linecache.clearcache"> <tt class="descclassname">linecache.</tt><tt class="descname">clearcache</tt><big>(</big><big>)</big><a class="headerlink" href="#linecache.clearcache" title="Permalink to this definition">¶</a></dt> <dd>Clear the cache. Use this function if you no longer need lines from files previously read using <a title="linecache.getline" class="reference internal" href="#linecache.getline"><tt class="xref docutils literal"><span class="pre">getline()</span></tt></a>.</dd></dl> <dl class="function"> <dt id="linecache.checkcache"> <tt class="descclassname">linecache.</tt><tt class="descname">checkcache</tt><big>(</big><span class="optional">[</span><em>filename</em><span class="optional">]</span><big>)</big><a class="headerlink" href="#linecache.checkcache" title="Permalink to this definition">¶</a></dt> <dd>Check the cache for validity. Use this function if files in the cache may have changed on disk, and you require the updated version. If <em>filename</em> is omitted, it will check all the entries in the cache.</dd></dl> <p>Example:</p> <div class="highlight-python"><div class="highlight"><pre><span class="gp">>>> </span><span class="kn">import</span> <span class="nn">linecache</span> <span class="gp">>>> </span><span class="n">linecache</span><span class="o">.</span><span class="n">getline</span><span class="p">(</span><span class="s">'/etc/passwd'</span><span class="p">,</span> <span class="mi">4</span><span class="p">)</span> <span class="go">'sys:x:3:3:sys:/dev:/bin/sh\n'</span> </pre></div> </div> </div> </div> </div> </div> <div class="sphinxsidebar"> <div class="sphinxsidebarwrapper"> <h4>Previous topic</h4> <p class="topless"><a href="fnmatch.html" title="previous chapter">11.8. <tt class="docutils literal docutils literal docutils literal"><span class="pre">fnmatch</span></tt> — Unix filename pattern matching</a></p> <h4>Next topic</h4> <p class="topless"><a href="shutil.html" title="next chapter">11.10. <tt class="docutils literal docutils literal"><span class="pre">shutil</span></tt> — High-level file operations</a></p> <h3>This Page</h3> <ul class="this-page-menu"> <li><a href="../_sources/library/linecache.txt" rel="nofollow">Show Source</a></li> </ul> <div id="searchbox" style="display: none"> <h3>Quick search</h3> <form class="search" action="../search.html" method="get"> <input type="text" name="q" size="18" /> <input type="submit" value="Go" /> <input type="hidden" name="check_keywords" value="yes" /> <input type="hidden" name="area" value="default" /> </form> <p class="searchtip" style="font-size: 90%"> Enter search terms or a module, class or function name. </p> </div> <script type="text/javascript">$('#searchbox').show(0);</script> </div> </div> <div class="clearer"></div> </div> <div class="related"> <h3>Navigation</h3> <ul> <li class="right" style="margin-right: 10px"> <a href="../genindex.html" title="General Index" >index</a></li> <li class="right" > <a href="../modindex.html" title="Global Module Index" >modules</a> |</li> <li class="right" > <a href="shutil.html" title="11.10. shutil — High-level file operations" >next</a> |</li> <li class="right" > <a href="fnmatch.html" title="11.8. fnmatch — Unix filename pattern matching" >previous</a> |</li> <li><img src="../_static/py.png" alt="" style="vertical-align: middle; margin-top: -1px"/></li> <li><a href="../index.html">Python v2.6.5 documentation</a> »</li> <li><a href="index.html" >The Python Standard Library</a> »</li> <li><a href="filesys.html" >11. File and Directory Access</a> »</li> </ul> </div> <div class="footer"> © <a href="../copyright.html">Copyright</a> 1990-2010, Python Software Foundation. <br /> The Python Software Foundation is a non-profit corporation. <a href="http://www.python.org/psf/donations/">Please donate.</a> <br /> Last updated on Mar 19, 2010. Created using <a href="http://sphinx.pocoo.org/">Sphinx</a> 0.6.5. </div> </body> </html>