10.9. linecache — Acesso aleatório a linhas de texto¶
Código Fonte: Lib/linecache.py
The linecache module allows one to get any line from any 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 module
to retrieve source lines for inclusion in the formatted traceback.
O módulo linecache define as seguintes funções:
-
linecache.getline(filename, lineno[, module_globals])¶ Obtém a linha lineno do arquivo chamado filename. Essa função nunca levanta uma exceção — ela retornará
''em erros (o caractere de nova linha final será incluído para as linhas encontradas).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__loader__in module_globals, in case the module was imported from a zipfile or other non-filesystem import source.Novo na versão 2.5: The module_globals parameter was added.
-
linecache.clearcache()¶ Limpa o cache. Use esta função se você não precisar mais de linhas de arquivos lidos anteriormente usando
getline().
-
linecache.checkcache([filename])¶ Verifica a validade do cache. Use esta função se os arquivos no cache tiverem sido alterados no disco e você precisar da versão atualizada. Se filename for omitido, ele verificará todas as entradas no cache.
Exemplo:
>>> import linecache
>>> linecache.getline('/etc/passwd', 4)
'sys:x:3:3:sys:/dev:/bin/sh\n'
