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. 弄清 CSS3 的 transition 和 animation

    弄清 CSS3 的 transition 和 animation transition transition 属性是 transition-property, transition-duration, ...

  2. 怎么统计指定文件夹下含有.xml格式的文件数目

    如何统计指定文件夹下含有.xml格式的文件数目?如题 ------解决思路----------------------Directory.GetFiles(@"路径", " ...

  3. php随机ip

    $ip_long = array( array('607649792', '608174079'), //36.56.0.0-36.63.255.255 array('1038614528', '10 ...

  4. node、npm、gulp、bower、ionic下载及安装

    node: http://nodejs.cn/ npm: 随node一起安装了 gulp: http://www.gulpjs.com.cn/docs/getting-started/ bower: ...

  5. CentOS7 编译安装LVS 互为主备 (实测 笔记 Centos 7.0 + ipvsadm 1.27 + keepalived 1.2.15 )

    环境: 系统硬件:vmware vsphere (CPU:2*4核,内存2G,双网卡) LVS服务器(两台): 系统:Centos7.0 64位(LVS+keepalived) LvsMaster:1 ...

  6. hadoop生态圈介绍

    原文地址:大数据技术Hadoop入门理论系列之一----hadoop生态圈介绍   1. hadoop 生态概况 Hadoop是一个由Apache基金会所开发的分布式系统基础架构. 用户可以在不了解分 ...

  7. ANT_HOME is set incorrectly or ant could not be located .Please set ANT_HOME.

    今天配置ant,奶奶的老是报错. 因为环境变量 ANT_HOME 和 JAVA_HOME 设置值不能有“;” ,把分号去掉即可成功了. 神奇!

  8. SQL存储过程基础(从基础开始学,加油!)

    Transact-SQL中的存储过程,非常类似于Java语言中的方法,它可以重复调用.当存储过程执行一次后,可以将语句缓存中,这样下次执行的时候直接使用缓存中的语句.这样就可以提高存储过程的性能. Ø ...

  9. offsetTop、clientTop、scrollTop、offsetTop属性

    HTML精确定位:scrollLeft,scrollWidth,clientWidth,offsetWidth scrollHeight: 获取对象的滚动高度. scrollLeft:设置或获取位于对 ...

  10. checkbox选中状态不被改变

    让它的状态只能看不能改变,加上onclick="return false;". 也可以disabled="true";但是这个颜色变淡了; <input ...