Fred Drake | 21572fd | 1999-06-14 19:47:47 +0000 | [diff] [blame] | 1 | \section{\module{linecache} --- |
Fred Drake | 97c2fa0 | 1999-06-17 16:38:18 +0000 | [diff] [blame] | 2 | Random access to text lines} |
Fred Drake | 21572fd | 1999-06-14 19:47:47 +0000 | [diff] [blame] | 3 | |
| 4 | \declaremodule{standard}{linecache} |
Fred Drake | 57657bc | 2000-12-01 15:25:23 +0000 | [diff] [blame] | 5 | \sectionauthor{Moshe Zadka}{moshez@zadka.site.co.il} |
Fred Drake | 97c2fa0 | 1999-06-17 16:38:18 +0000 | [diff] [blame] | 6 | \modulesynopsis{This module provides random access to individual lines |
| 7 | from text files.} |
Fred Drake | 21572fd | 1999-06-14 19:47:47 +0000 | [diff] [blame] | 8 | |
| 9 | |
| 10 | The \module{linecache} module allows one to get any line from any file, |
| 11 | while attempting to optimize internally, using a cache, the common case |
Fred Drake | 97c2fa0 | 1999-06-17 16:38:18 +0000 | [diff] [blame] | 12 | where many lines are read from a single file. This is used by the |
| 13 | \refmodule{traceback} module to retrieve source lines for inclusion in |
| 14 | the formatted traceback. |
Fred Drake | 21572fd | 1999-06-14 19:47:47 +0000 | [diff] [blame] | 15 | |
| 16 | The \module{linecache} module defines the following functions: |
| 17 | |
Phillip J. Eby | 4703211 | 2006-04-11 01:07:43 +0000 | [diff] [blame] | 18 | \begin{funcdesc}{getline}{filename, lineno\optional{, module_globals}} |
Fred Drake | 21572fd | 1999-06-14 19:47:47 +0000 | [diff] [blame] | 19 | Get line \var{lineno} from file named \var{filename}. This function |
Fred Drake | 38e5d27 | 2000-04-03 20:13:55 +0000 | [diff] [blame] | 20 | will never throw an exception --- it will return \code{''} on errors |
| 21 | (the terminating newline character will be included for lines that are |
| 22 | found). |
Fred Drake | 21572fd | 1999-06-14 19:47:47 +0000 | [diff] [blame] | 23 | |
| 24 | If a file named \var{filename} is not found, the function will look |
Fred Drake | 97c2fa0 | 1999-06-17 16:38:18 +0000 | [diff] [blame] | 25 | for it in the module\indexiii{module}{search}{path} search path, |
Phillip J. Eby | 678b8ec | 2006-04-11 01:15:28 +0000 | [diff] [blame] | 26 | \code{sys.path}, after first checking for a \pep{302} \code{__loader__} |
Phillip J. Eby | 4703211 | 2006-04-11 01:07:43 +0000 | [diff] [blame] | 27 | in \var{module_globals}, in case the module was imported from a zipfile |
| 28 | or other non-filesystem import source. |
| 29 | |
| 30 | \versionadded[The \var{module_globals} parameter was added]{2.5} |
Fred Drake | 21572fd | 1999-06-14 19:47:47 +0000 | [diff] [blame] | 31 | \end{funcdesc} |
| 32 | |
| 33 | \begin{funcdesc}{clearcache}{} |
Fred Drake | 38e5d27 | 2000-04-03 20:13:55 +0000 | [diff] [blame] | 34 | Clear the cache. Use this function if you no longer need lines from |
| 35 | files previously read using \function{getline()}. |
Fred Drake | 21572fd | 1999-06-14 19:47:47 +0000 | [diff] [blame] | 36 | \end{funcdesc} |
| 37 | |
Hye-Shik Chang | 182ac85 | 2004-10-26 09:16:42 +0000 | [diff] [blame] | 38 | \begin{funcdesc}{checkcache}{\optional{filename}} |
Fred Drake | 97c2fa0 | 1999-06-17 16:38:18 +0000 | [diff] [blame] | 39 | Check the cache for validity. Use this function if files in the cache |
Hye-Shik Chang | 182ac85 | 2004-10-26 09:16:42 +0000 | [diff] [blame] | 40 | may have changed on disk, and you require the updated version. If |
| 41 | \var{filename} is omitted, it will check the whole cache entries. |
Fred Drake | 21572fd | 1999-06-14 19:47:47 +0000 | [diff] [blame] | 42 | \end{funcdesc} |
| 43 | |
| 44 | Example: |
| 45 | |
| 46 | \begin{verbatim} |
| 47 | >>> import linecache |
| 48 | >>> linecache.getline('/etc/passwd', 4) |
Ka-Ping Yee | fa004ad | 2001-01-24 17:19:08 +0000 | [diff] [blame] | 49 | 'sys:x:3:3:sys:/dev:/bin/sh\n' |
Fred Drake | 21572fd | 1999-06-14 19:47:47 +0000 | [diff] [blame] | 50 | \end{verbatim} |