Python linecache、glob模組,linecacheglob
今天學習了兩個好玩的模組,linecache、glob模組
linecache模組
在python中,有個好用的模組linecache,該模組允許從任何檔案裡得到任何的行,並且使用緩衝進行最佳化,常見的情況是從單個檔案讀取多行
#從linecache的名稱,我們可以知道該模組和cache(緩衝)有關
#linecache現把檔案讀入到緩衝中,在以後訪問檔案的時候,就不必要再從硬碟讀取
#所以經常用於那些讀取頻率很高的檔案
還可以參考:open()
linecache提供了如下幾個函數:
linecache.getlines(filename,module_globals=None)
從名為filename的檔案中得到全部內容,輸出為列表格式,以檔案每行為列表中的一個元素,並以linenum-1為元素在列表中的位置儲存
linecache.getline(filename, lineno,module_globals=None)
從名為filename的檔案中得到第lineno行。這個函數從不會拋出一個異常–產生錯誤時它將返回”(分行符號將包含在找到的行裡)
如果檔案沒有找到,這個函數將會在sys.path搜尋
linecache.clearcache()
清除緩衝。如果你不再需要先前從getline()中得到的行
linecache.checkcache(filename=None)
檢查緩衝的有效性。如果在緩衝中的檔案在硬碟上發生了變化,並且你需要更新版本,使用這個函數。如果省略filename,將檢查緩衝裡的所有條目
linecache.updatecache(filename ,module_globals=None)
更新檔案名稱為filename的緩衝。如果filename檔案更新了,使用這個函數可以更新linecache.getlines(filename)返回的列表
下面看一個例子:
import linecacheimport pprint # 建立一個檔案filename = 'linecacheTest.txt'myfile = open(filename, 'w')for i in range(1, 5): myfile.write('This is the '+str(i)+'th line\n')myfile.close() # 擷取所有的行pprint.pprint(linecache.getlines(filename)) # 擷取其中任意一行pprint.pprint(linecache.getline(filename,3)) # 擷取其中第3,4行pprint.pprint(linecache.getlines(filename)[2:4]) # 釋放緩衝linecache.clearcache()
結果是:
['This is the 1th line\n', 'This is the 2th line\n', 'This is the 3th line\n', 'This is the 4th line\n']'This is the 3th line\n'['This is the 3th line\n', 'This is the 4thline\n']
注意:使用linecache.getlines(filename)開啟檔案的內容之後,如果filename檔案發生了改變,如果要再次用linecache.getlines(filename)擷取的內容,不是檔案的最新內容,還是之前的內容,此時有兩種方法:
1、使用linecache.checkcache(filename)來更新檔案在硬碟上的緩衝,然後在執行linecache.getlines(filename)就可以擷取到a.txt的最新內容;
2、直接使用linecache.updatecache(filename),即可擷取最新的a.txt的最新內容
另外:
1)、讀取檔案之後,不需要使用檔案的緩衝時,需要在最後清理一下緩衝,使linecache.clearcache()清理緩衝,釋放緩衝
2)、此模組使用記憶體來快取檔案內容,所以需要耗費記憶體,開啟檔案的大小和開啟速度和你的記憶體大小有關係
glob模組
globbing是萬用字元的意思,這個模組的意思就是尋找符合特定規則的檔案路徑名
常用的萬用字元有下面幾個:
* matches everything
? matches any single character
[seq] matchesany character in seq
[!seq] matchesany character not in seq
glob模組提供了如下幾個函數:
glob.glob(pathname)
返回所有匹配的檔案路徑列表。它只有一個參數pathname,定義了檔案路徑匹配規則,這裡可以是絕對路徑,也可以是相對路徑,可以使用萬用字元。
glob.iglob(pathname)
擷取一個可遍曆的對象,使用它可以逐個擷取匹配的檔案路徑名。與glob.glob()的區別是:glob.glob同時擷取所有的匹配路徑,而 glob.iglob一次只擷取一個匹配路徑,一般用於迴圈處理每個路徑
glob.escape(pathname)
忽略所有的萬用字元,如果檔案名稱中含有萬用字元,但又不想一個一個的使用’\’進行轉義,那麼就使用這個函數,忽略掉所有的萬用字元
下面看這個例子:
>>> import glob>>> print(glob.glob('a*.*'))['autohomehtml.html', 'autohomeParser.py','autotemp.txt', 'autotempfile1.txt']>>> print(glob.glob('*.py'))['autohomeParser.py','beautifulSoupTest.py', 'collectionsTest.py', 'itertoolsTest.py','linecacheTest.py', 'linecacheTest_forBlog.py', 'lxmlTest.py', 'myRe.py','PyQtTest.py', 'requestsTest.py', 'tablibTest.py', 'timeitTest.py','urllibtest.py']>>> print(glob.glob('*[0-9]*.*'))['autotempfile1.txt']>>> print(glob.glob('*.txt'))['autotemp.txt', 'autotempfile1.txt','linecacheTest.txt', 'linecachetext.txt', 'mypage.txt', '安裝scrapy.txt']>>> for i in glob.iglob('*.py'): print(i) autohomeParser.pybeautifulSoupTest.pycollectionsTest.pyitertoolsTest.pylinecacheTest.pylinecacheTest_forBlog.pylxmlTest.pymyRe.pyPyQtTest.pyrequestsTest.pytablibTest.pytimeitTest.pyurllibtest.py