熱線電話:13121318867

登錄
首頁精彩閱讀開眼界!Python遍歷文件可以這樣做
開眼界!Python遍歷文件可以這樣做
2021-03-23
收藏

來源:【公眾號】

Python技術

開眼界!Python遍歷文件可以這樣做

Python 對于文件夾或者文件的遍歷一般有兩種操作方法,一種是至二級利用其封裝好的 walk 方法操作:

import os for root,dirs,files in os.walk("/Users/cxhuan/Downloads/globtest/hello"):
    for dir in dirs:
        print(os.path.join(root, dir))
    for file in files:
        print(os.path.join(root, file))

上面代碼運行結果如下:

/Users/cxhuan/Downloads/globtest/hello/world /Users/cxhuan/Downloads/globtest/hello/.DS_Store
/Users/cxhuan/Downloads/globtest/hello/hello3.txt
/Users/cxhuan/Downloads/globtest/hello/hello2.txt
/Users/cxhuan/Downloads/globtest/hello/hello1.txt
/Users/cxhuan/Downloads/globtest/hello/world/world1.txt
/Users/cxhuan/Downloads/globtest/hello/world/world3.txt
/Users/cxhuan/Downloads/globtest/hello/world/world2.txt

上述程序,將 os.walk 讀取到的所有路徑 root 、目錄名 dirs 與文件名 files ,也就是三個文件數組利用 foreach 循環輸出。join方法就是將其路徑與目錄名或者文件名連接起來,組成一個完整的目錄。

另一種是用遞歸的思路,寫成下面的形式:

import os files = list()
def dirAll(pathname):
    if os.path.exists(pathname):
        filelist = os.listdir(pathname)
        for f in filelist:
            f = os.path.join(pathname, f)
            if os.path.isdir(f):
                dirAll(f)
            else:
                dirname = os.path.dirname(f)
                baseName = os.path.basename(f)
                if dirname.endswith(os.sep):
                    files.append(dirname+baseName)
                else:
                    files.append(dirname+os.sep+baseName)


dirAll("/Users/cxhuan/Downloads/globtest/hello") for f in files:
    print(f)

運行上面代碼,得到的結果和上面一樣。

這兩種方法都沒問題,就是寫起來比較麻煩,特別是第二種,一不小心還有可能寫出 bug 。

今天我們來介紹第三種方法——利用 glob 模塊來遍歷文件。

簡介

glob 是 python 自帶的一個操作文件的模塊,以簡潔實用著稱。由于這個模塊的功能比較簡單,所以也很容易上手和使用。它主要用來查找符合特定規則的文件路徑。使用這個模塊來查找文件,只需要用到*、? 和 [] 這三個匹配符:

 * : 匹配0個或多個字符;
 ? : 匹配單個字符;
 [] :匹配指定范圍內的字符,如:[0-9]匹配數字。

glob.glob 方法

glob.glob 方法主要返回所有匹配的文件路徑列表。它只有一個參數 pathname ,定義了文件路徑匹配規則,這里可以是絕對路徑,也可以是相對路徑。

使用 * 匹配

我們可以用 * 匹配零個或者多個字符。

輸出目錄下的子目錄或者文件:

for p1 in glob.glob('/Users/cxhuan/Downloads/globtest/*'):
    print(p1)

運行上面代碼,會將 globtest 文件夾下僅有的目錄輸出出來,輸出內容如下:

/Users/cxhuan/Downloads/globtest/hello

我們也可以通過制定層級來遍歷文件或者文件夾:

for p in glob.glob('/Users/cxhuan/Downloads/globtest/*/*'):
    print(p)

上面的代碼會遍歷 globtest 文件夾以及子文件夾,將所有的文件或文件夾路徑打印出來:

/Users/cxhuan/Downloads/globtest/hello/world
/Users/cxhuan/Downloads/globtest/hello/hello3.txt
/Users/cxhuan/Downloads/globtest/hello/hello2.txt
/Users/cxhuan/Downloads/globtest/hello/hello1.txt

我們也可以對文件或者文件夾進行過濾:

for p in glob.glob('/Users/cxhuan/Downloads/globtest/hello/*3.txt'):
    print(p)

上面代碼值匹配 hello 目錄下的文件名末尾為 ‘3’ 的 txt 文件,運行結果如下:

/Users/cxhuan/Downloads/globtest/hello/hello3.txt

使用 ? 匹配

我們可以用問號(?)匹配任何單個的字符。

for p in glob.glob('/Users/cxhuan/Downloads/globtest/hello/hello?.txt'):
    print(p)

上面的代碼輸出 hello 目錄下的以 ‘hello’ 開頭的 txt 文件,輸出結果如下:

/Users/cxhuan/Downloads/globtest/hello/hello3.txt
/Users/cxhuan/Downloads/globtest/hello/hello2.txt
/Users/cxhuan/Downloads/globtest/hello/hello1.txt

使用 [] 匹配

我們可以使用 [] 來匹配一個范圍:

for p in glob.glob('/Users/cxhuan/Downloads/globtest/hello/*[0-2].*'):
    print(p)

我們想要得到 hello 目錄下的文件名結尾數字的范圍為 0到2的文件,運行上面代碼,獲得的輸出為:

/Users/cxhuan/Downloads/globtest/hello/hello2.txt
/Users/cxhuan/Downloads/globtest/hello/hello1.txt

glob.iglob 方法

python 的 glob 方法可以對文件夾下所有文件進行遍歷,并返回一個 list 列表。而 iglob 方法一次只獲取一個匹配路徑。下面是一個簡單的例子來說明二者的區別:

p = glob.glob('/Users/cxhuan/Downloads/globtest/hello/hello?.*') print(p) print('----------------------')

p = glob.iglob('/Users/cxhuan/Downloads/globtest/hello/hello?.*') print(p)

運行上面代碼,結果返回是:

['/Users/cxhuan/Downloads/globtest/hello/hello3.txt''/Users/cxhuan/Downloads/globtest/hello/hello2.txt',
 '/Users/cxhuan/Downloads/globtest/hello/hello1.txt'] ---------------------- <generator
 object _iglob at 0x1040d8ac0>

從上面的結果我們可以很容易看到二者的區別,前者返回的是一個列表,后者返回的是一個可迭代對象。

我們針對這個可迭代對象做一下操作看看:

p = glob.iglob('/Users/cxhuan/Downloads/globtest/hello/hello?.*') print(p.__next__()) print(p.__next__())

運行結果如下:

/Users/cxhuan/Downloads/globtest/hello/hello3.txt
/Users/cxhuan/Downloads/globtest/hello/hello2.txt

我們可以看到,針對這個可迭代對象,我們一次可以獲取到一個元素。這樣做的好處是節省內存,試想如果一個路徑下有大量的文件夾或者文件,我們使用這個迭代對象不用一次性全部獲取到內存,而是可以慢慢獲取。

數據分析咨詢請掃描二維碼

若不方便掃碼,搜微信號:CDAshujufenxi

數據分析師資訊
更多

OK
客服在線
立即咨詢
日韩人妻系列无码专区视频,先锋高清无码,无码免费视欧非,国精产品一区一区三区无码
客服在線
立即咨詢