You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

52 lines
1.8 KiB

  1. :mod:`linecache` --- Random access to text lines
  2. ================================================
  3. .. module:: linecache
  4. :synopsis: This module provides random access to individual lines from text files.
  5. .. sectionauthor:: Moshe Zadka <moshez@zadka.site.co.il>
  6. **Source code:** :source:`Lib/linecache.py`
  7. --------------
  8. The :mod:`linecache` module allows one to get any line from any file, while
  9. attempting to optimize internally, using a cache, the common case where many
  10. lines are read from a single file. This is used by the :mod:`traceback` module
  11. to retrieve source lines for inclusion in the formatted traceback.
  12. The :mod:`linecache` module defines the following functions:
  13. .. function:: getline(filename, lineno, module_globals=None)
  14. Get line *lineno* from file named *filename*. This function will never raise an
  15. exception --- it will return ``''`` on errors (the terminating newline character
  16. will be included for lines that are found).
  17. .. index:: triple: module; search; path
  18. If a file named *filename* is not found, the function will look for it in the
  19. module search path, ``sys.path``, after first checking for a :pep:`302`
  20. ``__loader__`` in *module_globals*, in case the module was imported from a
  21. zipfile or other non-filesystem import source.
  22. .. function:: clearcache()
  23. Clear the cache. Use this function if you no longer need lines from files
  24. previously read using :func:`getline`.
  25. .. function:: checkcache(filename=None)
  26. Check the cache for validity. Use this function if files in the cache may have
  27. changed on disk, and you require the updated version. If *filename* is omitted,
  28. it will check all the entries in the cache.
  29. Example::
  30. >>> import linecache
  31. >>> linecache.getline('/etc/passwd', 4)
  32. 'sys:x:3:3:sys:/dev:/bin/sh\n'