1.模块简介

linecache主要用于缓存文件内容,如果下次继续读取该文件,则不需要打开文件,直接在缓存中获取该文件内容。

2.模块使用

模块的基本方法有getline,clearcache,getlines,checkcache;

方法getline主要用于获取指定行的内容;

方法clearcache主要用于清空缓存;

方法getlines主要用于从缓存中获取文件所有的行,如果缓存中没有该文件内容,则更新缓存,如果更新缓存失败(例如文件太大),则返回空列表;

方法checkcache主要用于删除超时的缓存;

example,

import linecache
import os # 小文件名称
smallFileName = "BrowseQueryResult.txt"
# 大文件名称
bigFileName = "PaperID_mapping_to_AffiliationsID.txt" # 获取小文件第一行
samllLine1 = linecache.getline(smallFileName,1)
print "small file line 1:" + samllLine1.decode("gb2312")
# 获取小文件所有数据
cacheSmall = linecache.getlines(smallFileName)
print "samll file length = %d"%(len(cacheSmall))
print "small file size = %d KB"%(os.path.getsize(smallFileName) * 1.0 / (1024)) # 获取大文件所有数据
cacheBig = linecache.getlines(bigFileName)
print "big file length = %d"%(len(cacheBig))
print "big file size = %d MB"%(os.path.getsize(bigFileName) * 1.0 / ( 1024* 1024)) linecache.clearcache()

控制台输出,可以发现,当系统配置低的时候,linecache.getlines获取大文件时,会失败,这时候得到的是一个空列表,通过os.path.getsize可以观察到文件的大小。

small file line 1:教育技术学视野下的未来课堂研究        1769    教育|199||教育技术|53||教育技术学|22||未来课堂|13||技术|12||教育技术学视野下的未来课堂研究|7||课堂教学模式|5||云计算|5||教学模式|5||课堂
|5||发展性教学|4||信息化教学模式|4||教育技术 并含 技能|4||颠倒课堂|4||课堂教学|4||在线教育|4||未来教室|4||课堂互动|4||计算机|3||思维导图|3||智慧教室|3||信息化教学|3||教育技术 技能|3||教育技术学视野下
的未来课堂|3||毕业论文|3||电子书包|3||合作学习|2||互联网|2||评价|2||提高远程教学交互实效的教学教法研究|2||教育技术研究方法|2||沉积物磷|2||数学 自主探究|2||教育技术技能|2||学习空间设计|2||教育技术发展|
2||电子商务|2||数字化校园|2||信息技术支持下的教育教学模式研究|2||情报 技术|2||末来课堂|2||模糊数学|2||绿色建筑|2||数学|2||心理学|2||物流|2||泛在学习|2||财务管理|2||未来|2||信息技术|2 samll file length = 100
small file size = 103 KB
big file length = 0
big file size = 3219 MB

3.源码分析

linecache源码所在路径为Python-2.7.10\Lib\linecache.py,

源码如下,

"""Cache lines from files.

This is intended to read lines from modules imported -- hence if a filename
is not found, it will look down the module search path for a file by
that name.
""" import sys
import os __all__ = ["getline", "clearcache", "checkcache"] # 获取指定行的内容
def getline(filename, lineno, module_globals=None):
# 利用getlines获取所有行
lines = getlines(filename, module_globals)
# 如果指定行在文件总行数范围之内,则返回相应的该行的数据
if 1 <= lineno <= len(lines):
return lines[lineno-1]
else:
return '' # cache的数据格式为cache[filename] = size, mtime, lines, fullname
# filename为文件名
# size为文件大小
# mtime为文件修改时间
# lines为文件所有的数据
# fullname为文件的全名
cache = {} # The cache # 清空缓存
def clearcache():
"""Clear the cache entirely."""
# 引入全局变量cache
global cache
# 将cache设置为空
cache = {} # 从缓存中获取文件所有的行,如果缓存中没有该文件内容,则更新缓存,如果更新缓存失败(例如文件太大),则返回空列表
def getlines(filename, module_globals=None):
"""Get the lines for a file from the cache.
Update the cache if it doesn't contain an entry for this file already.""" # 如果文件名在cache中,则返回cache中该文件的全部数据
if filename in cache:
return cache[filename][2] # 否则,更新cache
try:
return updatecache(filename, module_globals)
# 如果更新cache时,发生内存错误,则返回空列表
except MemoryError:
clearcache()
return [] # 删除超时的缓存cache
def checkcache(filename=None):
"""Discard cache entries that are out of date.
(This is not checked upon each call!)""" if filename is None:
filenames = cache.keys()
else:
if filename in cache:
filenames = [filename]
else:
return for filename in filenames:
size, mtime, lines, fullname = cache[filename]
if mtime is None:
continue # no-op for files loaded via a __loader__
# 获取cache中文件的修改时间
try:
stat = os.stat(fullname)
# auguries出错,则将cache中的该文件内容删除
except os.error:
del cache[filename]
continue
# 如果文件大小和修改时间均不相等,则将cache中的该文件内容删除
if size != stat.st_size or mtime != stat.st_mtime:
del cache[filename] # 更新缓存
def updatecache(filename, module_globals=None):
"""Update a cache entry and return its list of lines.
If something's wrong, print a message, discard the cache entry,
and return an empty list.""" # 如果文件名在缓存cache中,将cache中的该文件内容删除
if filename in cache:
del cache[filename]
# 如果文件名不合法,则返回空列表
if not filename or (filename.startswith('<') and filename.endswith('>')):
return [] # 将文件名设置为文件名全称
fullname = filename
# 获取该文件名全称的状态
try:
stat = os.stat(fullname)
# 如果出错,则将文件名设置为基本文件名
except OSError:
basename = filename # Try for a __loader__, if available
if module_globals and '__loader__' in module_globals:
name = module_globals.get('__name__')
loader = module_globals['__loader__']
get_source = getattr(loader, 'get_source', None) if name and get_source:
try:
data = get_source(name)
except (ImportError, IOError):
pass
else:
if data is None:
# No luck, the PEP302 loader cannot find the source
# for this module.
return []
cache[filename] = (
len(data), None,
[line+'\n' for line in data.splitlines()], fullname
)
return cache[filename][2] # Try looking through the module search path, which is only useful
# when handling a relative filename.
if os.path.isabs(filename):
return [] # 从系统路径中获取目录路径
for dirname in sys.path:
# When using imputil, sys.path may contain things other than
# strings; ignore them when it happens.
try:
fullname = os.path.join(dirname, basename)
except (TypeError, AttributeError):
# Not sufficiently string-like to do anything useful with.
continue
try:
stat = os.stat(fullname)
break
except os.error:
pass
else:
return []
# 通过file.readlines()读取文件的所有内容
try:
with open(fullname, 'rU') as fp:
lines = fp.readlines()
# 读取失败,则返回空列表
except IOError:
return []
if lines and not lines[-1].endswith('\n'):
lines[-1] += '\n'
size, mtime = stat.st_size, stat.st_mtime
# 以filename为key,(size, mtime, lines, fullname)为value
cache[filename] = size, mtime, lines, fullname
return lines

Python标准模块--linecache的更多相关文章

  1. Python标准模块--threading

    1 模块简介 threading模块在Python1.5.2中首次引入,是低级thread模块的一个增强版.threading模块让线程使用起来更加容易,允许程序同一时间运行多个操作. 不过请注意,P ...

  2. Python标准模块--logging

    1 logging模块简介 logging模块是Python内置的标准模块,主要用于输出运行日志,可以设置输出日志的等级.日志保存路径.日志文件回滚等:相比print,具备如下优点: 可以通过设置不同 ...

  3. Python标准模块--importlib

    作者:zhbzz2007 出处:http://www.cnblogs.com/zhbzz2007 欢迎转载,也请保留这段声明.谢谢! 1 模块简介 Python提供了importlib包作为标准库的一 ...

  4. Thread类的其他方法,同步锁,死锁与递归锁,信号量,事件,条件,定时器,队列,Python标准模块--concurrent.futures

    参考博客: https://www.cnblogs.com/xiao987334176/p/9046028.html 线程简述 什么是线程?线程是cpu调度的最小单位进程是资源分配的最小单位 进程和线 ...

  5. python 全栈开发,Day42(Thread类的其他方法,同步锁,死锁与递归锁,信号量,事件,条件,定时器,队列,Python标准模块--concurrent.futures)

    昨日内容回顾 线程什么是线程?线程是cpu调度的最小单位进程是资源分配的最小单位 进程和线程是什么关系? 线程是在进程中的 一个执行单位 多进程 本质上开启的这个进程里就有一个线程 多线程 单纯的在当 ...

  6. 【转】Python标准模块--importlib

    [转]Python标准模块--importlib 作者:zhbzz2007 出处:http://www.cnblogs.com/zhbzz2007 欢迎转载,也请保留这段声明.谢谢! 1 模块简介 P ...

  7. Python标准模块--logging(转载)

    转载地址:http://www.cnblogs.com/zhbzz2007/p/5943685.html#undefined Python标准模块--logging 1 logging模块简介 log ...

  8. python全栈开发,Day42(Thread类的其他方法,同步锁,死锁与递归锁,信号量,事件,条件,定时器,队列,Python标准模块--concurrent.futures)

    昨日内容回顾 线程 什么是线程? 线程是cpu调度的最小单位 进程是资源分配的最小单位 进程和线程是什么关系? 线程是在进程中的一个执行单位 多进程 本质上开启的这个进程里就有一个线程 多线程 单纯的 ...

  9. python标准模块(二)

    本文会涉及到的模块: json.pickle urllib.Requests xml.etree configparser shutil.zipfile.tarfile 1. json & p ...

随机推荐

  1. ACM: ICPC/CCPC Sudoku DFS - 数独

    Sudoku Time Limit : 3000/1000ms (Java/Other)   Memory Limit : 65535/65535K (Java/Other) Total Submis ...

  2. html设置透明度

    在html中,设置元素的透明度 在css相应元素中添加下面两行代码即可. filter:alpha(opacity=50); /*仅在ie中支持*/ opacity:0.5; /*不支持ie*/ op ...

  3. [基础技能] 安全技术——哈希算法密码破解之彩虹表(Rainbow Table)学习

    1.基础知识 刚刚学习过数字签名的相关知识,以及数字签名的伪造技术,而伪造数字签名归根结底就是密码破解的一个过程,然而直接破解的速度是非常缓慢的,所以有人想出一种办法,直接建立出一个数据文件,里面事先 ...

  4. C语言_第五章__实践(密码转换)

    1.   要求 输入China  输出 Glmre #include <stdio.h> #include <stdlib.h> int main() { char c ; c ...

  5. PO,VO,BO,DTO,POJO(POCO),DAO的区别(转载)

    PO:persistant object持久对象 最形象的理解就是一个PO就是数据库中的一条记录.好处是可以把一条记录作为一个对象处理,可以方便的转为其它对象. BO:business object业 ...

  6. 《JavaScript Dom编程艺术》(第二版)

    第一章<JavaScript简史> 1.JavaScript是一种脚本语言,通常只能通过Web浏览器去完成一些操作而不能像普通意义上的程序那样独立运行,它需要由Web浏览器进行解释和执行. ...

  7. 运行错误:error while loading shared libraries: xxx.so.0:cannot open shared object file: No such file or

    链接时可以通过-L和-l来指定自己的库,因此链接可以通过,但是运行时,系统仍无法找到指定的库,需要简单配置一下. 解决方法: 可以直接在将自己的库所在路径添加到/etc/ld.so.conf文件中.但 ...

  8. mui框架中底部导航的跳转1

    mui框架极大的方便了app的开发但是我们在做页面之间的切换时发现不能实现 a 链接的跳转,这是应为mui相关的一些控件是通过拦截a标签上的href来实现的,所以mui.js会阻止a标签上的href跳 ...

  9. Logstash之multiline 插件

    input { stdin { codec => multiline { pattern => "^\[" negate => true what => & ...

  10. Kotlin笔记

    官网: http://kotlinlang.org/ http://kotlinlang.org/docs/reference/ 中文教程: http://kotlindoc.com/ Gradle: ...