blob: fb71d7a3f3b63b612c786b182db14e10e6187784 [file] [log] [blame]
Fred Drake21572fd1999-06-14 19:47:47 +00001\section{\module{linecache} ---
Fred Drake97c2fa01999-06-17 16:38:18 +00002 Random access to text lines}
Fred Drake21572fd1999-06-14 19:47:47 +00003
4\declaremodule{standard}{linecache}
5\sectionauthor{Moshe Zadka}{mzadka@geocities.com}
Fred Drake97c2fa01999-06-17 16:38:18 +00006\modulesynopsis{This module provides random access to individual lines
7 from text files.}
Fred Drake21572fd1999-06-14 19:47:47 +00008
9
10The \module{linecache} module allows one to get any line from any file,
11while attempting to optimize internally, using a cache, the common case
Fred Drake97c2fa01999-06-17 16:38:18 +000012where many lines are read from a single file. This is used by the
13\refmodule{traceback} module to retrieve source lines for inclusion in
14the formatted traceback.
Fred Drake21572fd1999-06-14 19:47:47 +000015
16The \module{linecache} module defines the following functions:
17
18\begin{funcdesc}{getline}{filename, lineno}
19Get line \var{lineno} from file named \var{filename}. This function
20will never throw an exception --- it will return \code{''} on errors.
21
22If a file named \var{filename} is not found, the function will look
Fred Drake97c2fa01999-06-17 16:38:18 +000023for it in the module\indexiii{module}{search}{path} search path,
24\code{sys.path}.
Fred Drake21572fd1999-06-14 19:47:47 +000025\end{funcdesc}
26
27\begin{funcdesc}{clearcache}{}
Fred Drake97c2fa01999-06-17 16:38:18 +000028Clear the cache. Use this function if you know that you do not need
29to read lines from many of files you already read from using this
30module.
Fred Drake21572fd1999-06-14 19:47:47 +000031\end{funcdesc}
32
33\begin{funcdesc}{checkcache}{}
Fred Drake97c2fa01999-06-17 16:38:18 +000034Check the cache for validity. Use this function if files in the cache
35may have changed on disk, and you require the updated version.
Fred Drake21572fd1999-06-14 19:47:47 +000036\end{funcdesc}
37
38Example:
39
40\begin{verbatim}
41>>> import linecache
42>>> linecache.getline('/etc/passwd', 4)
43'sys:x:3:3:sys:/dev:/bin/sh\012'
44\end{verbatim}