linecache
linecache是专门支持读取大文件,而且支持行式读取的函数库。 linecache 预先把文件读入缓存起来,后面如果你访问该文件的话就不再从硬盘读取。对于大文件的读取效率还不错
Help on module linecache: NAME linecache - Cache lines from files. FILE /usr/local/lib/python2.7/linecache.py MODULE DOCS http://docs.python.org/library/linecache DESCRIPTION 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. FUNCTIONS checkcache(filename=None) Discard cache entries that are out of date. (This is not checked upon each call!) clearcache() Clear the cache entirely. getline(filename, lineno, module_globals=None) DATA __all__ = ['getline', 'clearcache', 'checkcache']
常用的方法:
linecache.getlines(filename,) #接收一个文件对象,返回一个列表,列表中的每个元素就是文件中的一行. 和readlines()一样 linecache.getline(filename,lineno) #接收一个文件对象和一个行数,返回指定行数对应的行内容
>>> linecache.getlines('/etc/passwd') ['root:x:0:0:root:/root:/bin/bash\n', 'bin:x:1:1:bin:/bin:/sbin/nologin\n', 'daemon:x:2:2:daemon:/sbin:/sbin/nologin\n', 'adm:x:3:4:adm:/var/adm:/sbin/nologin\n', 'lp:x:4:7:lp:/var/spool/lpd:/sbin/nologin\n', 'sync:x:5:0:sync:/sbin:/bin/sync\n', 'shutdown:x:6:0:shutdown:/sbin:/sbin/shutdown\n', 'halt:x:7:0:halt:/sbin:/sbin/halt\n', 'mail:x:8:12:mail:/var/spool/mail:/sbin/nologin\n', 'uucp:x:10:14:uucp:/var/spool/uucp:/sbin/nologin\n', 'operator:x:11:0:operator:/root:/sbin/nologin\n', 'games:x:12:100:games:/usr/games:/sbin/nologin\n', 'gopher:x:13:30:gopher:/var/gopher:/sbin/nologin\n', 'ftp:x:14:50:FTP User:/var/ftp:/sbin/nologin\n', 'nobody:x:99:99:Nobody:/:/sbin/nologin\n', 'dbus:x:81:81:System message bus:/:/sbin/nologin\n', 'vcsa:x:69:69:virtual console memory owner:/dev:/sbin/nologin\n', 'abrt:x:173:173::/etc/abrt:/sbin/nologin\n', 'haldaemon:x:68:68:HAL daemon:/:/sbin/nologin\n', 'ntp:x:38:38::/etc/ntp:/sbin/nologin\n', 'saslauth:x:499:76:"Saslauthd user":/var/empty/saslauth:/sbin/nologin\n', 'postfix:x:89:89::/var/spool/postfix:/sbin/nologin\n', 'sshd:x:74:74:Privilege-separated SSH:/var/empty/sshd:/sbin/nologin\n', 'tcpdump:x:72:72::/:/sbin/nologin\n', 'ansibletest:x:500:500::/home/ansibletest:/bin/bash\n', 'mysql:x:27:27:MySQL Server:/var/lib/mysql:/bin/bash\n', 'zabbix:x:498:499:Zabbix Monitoring System:/var/lib/zabbix:/sbin/nologin\n', 'apache:x:48:48:Apache:/var/www:/sbin/nologin\n', 'elasticsearch:x:497:498:elasticsearch user:/home/elasticsearch:/sbin/nologin\n', 'redis:x:496:497:Redis Server:/var/lib/redis:/sbin/nologin\n', 'pythons:x:501:501::/home/pythons:/bin/bash\n']
>>> linecache.getline('/etc/passwd',10) 'uucp:x:10:14:uucp:/var/spool/uucp:/sbin/nologin\n' >>> linecache.getline('/etc/passwd',5) 'lp:x:4:7:lp:/var/spool/lpd:/sbin/nologin\n'