blob: f18b1cdac07ba4b4ac8f4c9997456580a05e76e6 [file] [log] [blame]
Georg Brandl116aa622007-08-15 14:28:22 +00001:mod:`linecache` --- Random access to text lines
2================================================
3
4.. module:: linecache
5 :synopsis: This module provides random access to individual lines from text files.
6.. sectionauthor:: Moshe Zadka <moshez@zadka.site.co.il>
7
Raymond Hettinger10480942011-01-10 03:26:08 +00008**Source code:** :source:`Lib/linecache.py`
Georg Brandl116aa622007-08-15 14:28:22 +00009
Raymond Hettinger4f707fd2011-01-10 19:54:11 +000010--------------
11
Victor Stinner93f06652015-03-18 14:14:42 +010012The :mod:`linecache` module allows one to get any line from a Python source file, while
Georg Brandl116aa622007-08-15 14:28:22 +000013attempting to optimize internally, using a cache, the common case where many
14lines are read from a single file. This is used by the :mod:`traceback` module
15to retrieve source lines for inclusion in the formatted traceback.
16
R David Murray63998a32015-03-20 11:31:38 -040017The :func:`tokenize.open` function is used to open files. This
Victor Stinner93f06652015-03-18 14:14:42 +010018function uses :func:`tokenize.detect_encoding` to get the encoding of the
R David Murray63998a32015-03-20 11:31:38 -040019file; in the absence of an encoding token, the file encoding defaults to UTF-8.
Victor Stinner93f06652015-03-18 14:14:42 +010020
Georg Brandl116aa622007-08-15 14:28:22 +000021The :mod:`linecache` module defines the following functions:
22
23
Georg Brandlcd7f32b2009-06-08 09:13:45 +000024.. function:: getline(filename, lineno, module_globals=None)
Georg Brandl116aa622007-08-15 14:28:22 +000025
Georg Brandl7cb13192010-08-03 12:06:29 +000026 Get line *lineno* from file named *filename*. This function will never raise an
Georg Brandl116aa622007-08-15 14:28:22 +000027 exception --- it will return ``''`` on errors (the terminating newline character
28 will be included for lines that are found).
29
30 .. index:: triple: module; search; path
31
32 If a file named *filename* is not found, the function will look for it in the
33 module search path, ``sys.path``, after first checking for a :pep:`302`
34 ``__loader__`` in *module_globals*, in case the module was imported from a
35 zipfile or other non-filesystem import source.
36
Georg Brandl116aa622007-08-15 14:28:22 +000037
38.. function:: clearcache()
39
40 Clear the cache. Use this function if you no longer need lines from files
41 previously read using :func:`getline`.
42
43
Georg Brandlcd7f32b2009-06-08 09:13:45 +000044.. function:: checkcache(filename=None)
Georg Brandl116aa622007-08-15 14:28:22 +000045
46 Check the cache for validity. Use this function if files in the cache may have
47 changed on disk, and you require the updated version. If *filename* is omitted,
48 it will check all the entries in the cache.
49
Georg Brandlcd7f32b2009-06-08 09:13:45 +000050
Georg Brandl116aa622007-08-15 14:28:22 +000051Example::
52
53 >>> import linecache
Victor Stinner376658f2015-03-18 14:16:50 +010054 >>> linecache.getline(linecache.__file__, 8)
55 'import sys\n'