python模塊之linecache


# -*- coding: utf-8 -*-
#python 27
#xiaodeng
#python模塊之linecache


import linecache
'''
>>> help(linecache)
Help on module linecache:
FUNCTIONS
    checkcache = extended_linecache_checkcache(filename=None, orig_checkcache=<function checkcache>)
        Extend linecache.checkcache to preserve the <pyshell#...> entries
        
        Rather than repeating the linecache code, patch it to save the
        <pyshell#...> entries, call the original linecache.checkcache()
        (skipping them), and then restore the saved entries.
        
        orig_checkcache is bound at definition time to the original
        method, allowing it to be patched.
    
    clearcache()
        Clear the cache entirely.
    
    getline(filename, lineno, module_globals=None)

DATA
    __all__ = ['getline', 'clearcache', 'checkcache'] 
'''


#從名為filename的文件中得到第lineno行。這個函數從不會拋出一個異常–產生錯誤時它將返回”(換行符將包含在找到的行里)。



#linecache.getline(filename,lineno)
#查找特定行(第一行)的數據,返回一個字符串
filename='1.txt'
re= linecache.getline(filename,1)
print re.strip()#因為本身帶有換行符



#得到所有行數據,返回一個list
re= linecache.getlines(filename)
print re



#獲取多少行的數據
re= linecache.getlines(filename)[0:4]
print re



#更新文件名為filename的緩存。如果filename文件更新了,使用這個函數可以更新linecache.getlines(filename)返回的列表
linecache.updatecache(filename)



#清理緩存
linecache.clearcache()

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM