Python标准模块--linecache
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的更多相关文章
- Python标准模块--threading
1 模块简介 threading模块在Python1.5.2中首次引入,是低级thread模块的一个增强版.threading模块让线程使用起来更加容易,允许程序同一时间运行多个操作. 不过请注意,P ...
- Python标准模块--logging
1 logging模块简介 logging模块是Python内置的标准模块,主要用于输出运行日志,可以设置输出日志的等级.日志保存路径.日志文件回滚等:相比print,具备如下优点: 可以通过设置不同 ...
- Python标准模块--importlib
作者:zhbzz2007 出处:http://www.cnblogs.com/zhbzz2007 欢迎转载,也请保留这段声明.谢谢! 1 模块简介 Python提供了importlib包作为标准库的一 ...
- Thread类的其他方法,同步锁,死锁与递归锁,信号量,事件,条件,定时器,队列,Python标准模块--concurrent.futures
参考博客: https://www.cnblogs.com/xiao987334176/p/9046028.html 线程简述 什么是线程?线程是cpu调度的最小单位进程是资源分配的最小单位 进程和线 ...
- python 全栈开发,Day42(Thread类的其他方法,同步锁,死锁与递归锁,信号量,事件,条件,定时器,队列,Python标准模块--concurrent.futures)
昨日内容回顾 线程什么是线程?线程是cpu调度的最小单位进程是资源分配的最小单位 进程和线程是什么关系? 线程是在进程中的 一个执行单位 多进程 本质上开启的这个进程里就有一个线程 多线程 单纯的在当 ...
- 【转】Python标准模块--importlib
[转]Python标准模块--importlib 作者:zhbzz2007 出处:http://www.cnblogs.com/zhbzz2007 欢迎转载,也请保留这段声明.谢谢! 1 模块简介 P ...
- Python标准模块--logging(转载)
转载地址:http://www.cnblogs.com/zhbzz2007/p/5943685.html#undefined Python标准模块--logging 1 logging模块简介 log ...
- python全栈开发,Day42(Thread类的其他方法,同步锁,死锁与递归锁,信号量,事件,条件,定时器,队列,Python标准模块--concurrent.futures)
昨日内容回顾 线程 什么是线程? 线程是cpu调度的最小单位 进程是资源分配的最小单位 进程和线程是什么关系? 线程是在进程中的一个执行单位 多进程 本质上开启的这个进程里就有一个线程 多线程 单纯的 ...
- python标准模块(二)
本文会涉及到的模块: json.pickle urllib.Requests xml.etree configparser shutil.zipfile.tarfile 1. json & p ...
随机推荐
- T-SQL Recipes之Customized Database Objects
The Problem 创建灵活自定义对象决非是一个简单的任务.比如HR想看每种工作职称在所有年限里面的入职累计情况 The Solution 我们一步一步来拆解吧: 获取入职年限的集合,如1999, ...
- Codeforces Round #257 (Div. 2)
A - Jzzhu and Children 找到最大的ceil(ai/m)即可 #include <iostream> #include <cmath> using name ...
- jQuery插件开发(溢出滚动)
声明:此程序仅针对手机端,简单的封装一个插件,意在记载插件的开发过程,如有错误及不足之处,还望即时指出. 移动开发的时候,我们经常会遇到滑动事件,众所周知手机端滑动主要依靠touch事件.最近接连遇到 ...
- 案例1.用Ajax实现用户名的校验
用Ajax实现用户名的校验 java的验证类 public class UserDao { public boolean checkUserName(String name) { //这里的name是 ...
- 解决nginx中proxy_pass到tomcat的session丢失问题
之前在配置tomcat的时候都是一个项目对应一个tomcat,也就是一个端口.最近需要把两个项目整合到同一个tomcat中,通过配置nginx让两个域名指向同一tomcat的不同项目.整合完毕后发现其 ...
- centos中docker mongodb 配置
安装docker,对于Centos7,如下: $ sudo yum update$ sudo yum -y install docker$ sudo systemctl start docker 首先 ...
- 使用HttpClient来异步发送POST请求并解析GZIP回应
.NET 4.5(C#): 使用HttpClient来异步发送POST请求并解析GZIP回应 在新的C# 5.0和.NET 4.5环境下,微软为C#加入了async/await,同时还加入新的Syst ...
- maven整理——初步
最近用到了maven,查找了很多资料,写这篇博文是为了记录maven的使用学习,也方便自己日后好查找. 在这里引用http://www.cnblogs.com/dcba1112/archive/201 ...
- MongoDB 由于目标计算机积极拒绝,无法连接 2014-07-25T11:00:48.634+0800 warning: Failed to connect to 127.0.0.1:27017, reason: errno:10061
转载自:http://www.cnblogs.com/xiaoit/p/3867573.html 1:启动MongoDB 2014-07-25T11:00:48.634+0800 warning: F ...
- python pickle和json的区别
pickle可以在python之间进行交互 json可以实现python与不同开发语言的交互 pickle可以序列化python中的任何数据类型 json只能序列化python中的常归数据类型(列表等 ...