💎一站式轻松地调用各大LLM模型接口,支持GPT4、智谱、星火、月之暗面及文生图 广告
### 导航 - [索引](../genindex.xhtml "总目录") - [模块](../py-modindex.xhtml "Python 模块索引") | - [下一页](shutil.xhtml "shutil --- High-level file operations") | - [上一页](fnmatch.xhtml "fnmatch --- Unix filename pattern matching") | - ![](https://box.kancloud.cn/a721fc7ec672275e257bbbfde49a4d4e_16x16.png) - [Python](https://www.python.org/) » - zh\_CN 3.7.3 [文档](../index.xhtml) » - [Python 标准库](index.xhtml) » - [文件和目录访问](filesys.xhtml) » - $('.inline-search').show(0); | # [`linecache`](#module-linecache "linecache: This module provides random access to individual lines from text files.") --- Random access to text lines **Source code:** [Lib/linecache.py](https://github.com/python/cpython/tree/3.7/Lib/linecache.py) \[https://github.com/python/cpython/tree/3.7/Lib/linecache.py\] - - - - - - The [`linecache`](#module-linecache "linecache: This module provides random access to individual lines from text files.") module allows one to get any line from a Python source 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 [`traceback`](traceback.xhtml#module-traceback "traceback: Print or retrieve a stack traceback.") module to retrieve source lines for inclusion in the formatted traceback. The [`tokenize.open()`](tokenize.xhtml#tokenize.open "tokenize.open") function is used to open files. This function uses [`tokenize.detect_encoding()`](tokenize.xhtml#tokenize.detect_encoding "tokenize.detect_encoding") to get the encoding of the file; in the absence of an encoding token, the file encoding defaults to UTF-8. The [`linecache`](#module-linecache "linecache: This module provides random access to individual lines from text files.") module defines the following functions: `linecache.``getline`(*filename*, *lineno*, *module\_globals=None*)Get line *lineno* from file named *filename*. This function will never raise an exception --- it will return `''` on errors (the terminating newline character will be included for lines that are found). If a file named *filename* is not found, the function will look for it in the module search path, `sys.path`, after first checking for a [**PEP 302**](https://www.python.org/dev/peps/pep-0302) \[https://www.python.org/dev/peps/pep-0302\]`__loader__` in *module\_globals*, in case the module was imported from a zipfile or other non-filesystem import source. `linecache.``clearcache`()Clear the cache. Use this function if you no longer need lines from files previously read using [`getline()`](#linecache.getline "linecache.getline"). `linecache.``checkcache`(*filename=None*)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 *filename* is omitted, it will check all the entries in the cache. `linecache.``lazycache`(*filename*, *module\_globals*)Capture enough detail about a non-file-based module to permit getting its lines later via [`getline()`](#linecache.getline "linecache.getline") even if *module\_globals* is `None` in the later call. This avoids doing I/O until a line is actually needed, without having to carry the module globals around indefinitely. 3\.5 新版功能. 示例: ``` >>> import linecache >>> linecache.getline(linecache.__file__, 8) 'import sys\n' ``` ### 导航 - [索引](../genindex.xhtml "总目录") - [模块](../py-modindex.xhtml "Python 模块索引") | - [下一页](shutil.xhtml "shutil --- High-level file operations") | - [上一页](fnmatch.xhtml "fnmatch --- Unix filename pattern matching") | - ![](https://box.kancloud.cn/a721fc7ec672275e257bbbfde49a4d4e_16x16.png) - [Python](https://www.python.org/) » - zh\_CN 3.7.3 [文档](../index.xhtml) » - [Python 标准库](index.xhtml) » - [文件和目录访问](filesys.xhtml) » - $('.inline-search').show(0); | © [版权所有](../copyright.xhtml) 2001-2019, Python Software Foundation. Python 软件基金会是一个非盈利组织。 [请捐助。](https://www.python.org/psf/donations/) 最后更新于 5月 21, 2019. [发现了问题](../bugs.xhtml)? 使用[Sphinx](http://sphinx.pocoo.org/)1.8.4 创建。