
來源:【公眾號(hào)】
Python技術(shù)
Python 對(duì)于文件夾或者文件的遍歷一般有兩種操作方法,一種是至二級(jí)利用其封裝好的 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))
上面代碼運(yùn)行結(jié)果如下:
/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 ,也就是三個(gè)文件數(shù)組利用 foreach 循環(huán)輸出。join方法就是將其路徑與目錄名或者文件名連接起來,組成一個(gè)完整的目錄。
另一種是用遞歸的思路,寫成下面的形式:
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)
運(yùn)行上面代碼,得到的結(jié)果和上面一樣。
這兩種方法都沒問題,就是寫起來比較麻煩,特別是第二種,一不小心還有可能寫出 bug 。
今天我們來介紹第三種方法——利用 glob 模塊來遍歷文件。
glob 是 python 自帶的一個(gè)操作文件的模塊,以簡(jiǎn)潔實(shí)用著稱。由于這個(gè)模塊的功能比較簡(jiǎn)單,所以也很容易上手和使用。它主要用來查找符合特定規(guī)則的文件路徑。使用這個(gè)模塊來查找文件,只需要用到*、? 和 [] 這三個(gè)匹配符:
* : 匹配0個(gè)或多個(gè)字符;
? : 匹配單個(gè)字符;
[] :匹配指定范圍內(nèi)的字符,如:[0-9]匹配數(shù)字。
glob.glob 方法主要返回所有匹配的文件路徑列表。它只有一個(gè)參數(shù) pathname ,定義了文件路徑匹配規(guī)則,這里可以是絕對(duì)路徑,也可以是相對(duì)路徑。
我們可以用 * 匹配零個(gè)或者多個(gè)字符。
輸出目錄下的子目錄或者文件:
for p1 in glob.glob('/Users/cxhuan/Downloads/globtest/*'):
print(p1)
運(yùn)行上面代碼,會(huì)將 globtest 文件夾下僅有的目錄輸出出來,輸出內(nèi)容如下:
/Users/cxhuan/Downloads/globtest/hello
我們也可以通過制定層級(jí)來遍歷文件或者文件夾:
for p in glob.glob('/Users/cxhuan/Downloads/globtest/*/*'):
print(p)
上面的代碼會(huì)遍歷 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
我們也可以對(duì)文件或者文件夾進(jìn)行過濾:
for p in glob.glob('/Users/cxhuan/Downloads/globtest/hello/*3.txt'):
print(p)
上面代碼值匹配 hello 目錄下的文件名末尾為 ‘3’ 的 txt 文件,運(yùn)行結(jié)果如下:
/Users/cxhuan/Downloads/globtest/hello/hello3.txt
我們可以用問號(hào)(?)匹配任何單個(gè)的字符。
for p in glob.glob('/Users/cxhuan/Downloads/globtest/hello/hello?.txt'):
print(p)
上面的代碼輸出 hello 目錄下的以 ‘hello’ 開頭的 txt 文件,輸出結(jié)果如下:
/Users/cxhuan/Downloads/globtest/hello/hello3.txt /Users/cxhuan/Downloads/globtest/hello/hello2.txt /Users/cxhuan/Downloads/globtest/hello/hello1.txt
我們可以使用 [] 來匹配一個(gè)范圍:
for p in glob.glob('/Users/cxhuan/Downloads/globtest/hello/*[0-2].*'):
print(p)
我們想要得到 hello 目錄下的文件名結(jié)尾數(shù)字的范圍為 0到2的文件,運(yùn)行上面代碼,獲得的輸出為:
/Users/cxhuan/Downloads/globtest/hello/hello2.txt /Users/cxhuan/Downloads/globtest/hello/hello1.txt
python 的 glob 方法可以對(duì)文件夾下所有文件進(jìn)行遍歷,并返回一個(gè) list 列表。而 iglob 方法一次只獲取一個(gè)匹配路徑。下面是一個(gè)簡(jiǎn)單的例子來說明二者的區(qū)別:
p = glob.glob('/Users/cxhuan/Downloads/globtest/hello/hello?.*') print(p) print('----------------------')
p = glob.iglob('/Users/cxhuan/Downloads/globtest/hello/hello?.*') print(p)
運(yùn)行上面代碼,結(jié)果返回是:
['/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>
從上面的結(jié)果我們可以很容易看到二者的區(qū)別,前者返回的是一個(gè)列表,后者返回的是一個(gè)可迭代對(duì)象。
我們針對(duì)這個(gè)可迭代對(duì)象做一下操作看看:
p = glob.iglob('/Users/cxhuan/Downloads/globtest/hello/hello?.*') print(p.__next__()) print(p.__next__())
運(yùn)行結(jié)果如下:
/Users/cxhuan/Downloads/globtest/hello/hello3.txt /Users/cxhuan/Downloads/globtest/hello/hello2.txt
我們可以看到,針對(duì)這個(gè)可迭代對(duì)象,我們一次可以獲取到一個(gè)元素。這樣做的好處是節(jié)省內(nèi)存,試想如果一個(gè)路徑下有大量的文件夾或者文件,我們使用這個(gè)迭代對(duì)象不用一次性全部獲取到內(nèi)存,而是可以慢慢獲取。
數(shù)據(jù)分析咨詢請(qǐng)掃描二維碼
若不方便掃碼,搜微信號(hào):CDAshujufenxi
LSTM 模型輸入長(zhǎng)度選擇技巧:提升序列建模效能的關(guān)鍵? 在循環(huán)神經(jīng)網(wǎng)絡(luò)(RNN)家族中,長(zhǎng)短期記憶網(wǎng)絡(luò)(LSTM)憑借其解決長(zhǎng)序列 ...
2025-07-11CDA 數(shù)據(jù)分析師報(bào)考條件詳解與準(zhǔn)備指南? ? 在數(shù)據(jù)驅(qū)動(dòng)決策的時(shí)代浪潮下,CDA 數(shù)據(jù)分析師認(rèn)證愈發(fā)受到矚目,成為眾多有志投身數(shù) ...
2025-07-11數(shù)據(jù)透視表中兩列相乘合計(jì)的實(shí)用指南? 在數(shù)據(jù)分析的日常工作中,數(shù)據(jù)透視表憑借其強(qiáng)大的數(shù)據(jù)匯總和分析功能,成為了 Excel 用戶 ...
2025-07-11尊敬的考生: 您好! 我們誠(chéng)摯通知您,CDA Level I和 Level II考試大綱將于 2025年7月25日 實(shí)施重大更新。 此次更新旨在確保認(rèn) ...
2025-07-10BI 大數(shù)據(jù)分析師:連接數(shù)據(jù)與業(yè)務(wù)的價(jià)值轉(zhuǎn)化者? ? 在大數(shù)據(jù)與商業(yè)智能(Business Intelligence,簡(jiǎn)稱 BI)深度融合的時(shí)代,BI ...
2025-07-10SQL 在預(yù)測(cè)分析中的應(yīng)用:從數(shù)據(jù)查詢到趨勢(shì)預(yù)判? ? 在數(shù)據(jù)驅(qū)動(dòng)決策的時(shí)代,預(yù)測(cè)分析作為挖掘數(shù)據(jù)潛在價(jià)值的核心手段,正被廣泛 ...
2025-07-10數(shù)據(jù)查詢結(jié)束后:分析師的收尾工作與價(jià)值深化? ? 在數(shù)據(jù)分析的全流程中,“query end”(查詢結(jié)束)并非工作的終點(diǎn),而是將數(shù) ...
2025-07-10CDA 數(shù)據(jù)分析師考試:從報(bào)考到取證的全攻略? 在數(shù)字經(jīng)濟(jì)蓬勃發(fā)展的今天,數(shù)據(jù)分析師已成為各行業(yè)爭(zhēng)搶的核心人才,而 CDA(Certi ...
2025-07-09【CDA干貨】單樣本趨勢(shì)性檢驗(yàn):捕捉數(shù)據(jù)背后的時(shí)間軌跡? 在數(shù)據(jù)分析的版圖中,單樣本趨勢(shì)性檢驗(yàn)如同一位耐心的偵探,專注于從單 ...
2025-07-09year_month數(shù)據(jù)類型:時(shí)間維度的精準(zhǔn)切片? ? 在數(shù)據(jù)的世界里,時(shí)間是最不可或缺的維度之一,而year_month數(shù)據(jù)類型就像一把精準(zhǔn) ...
2025-07-09CDA 備考干貨:Python 在數(shù)據(jù)分析中的核心應(yīng)用與實(shí)戰(zhàn)技巧? ? 在 CDA 數(shù)據(jù)分析師認(rèn)證考試中,Python 作為數(shù)據(jù)處理與分析的核心 ...
2025-07-08SPSS 中的 Mann-Kendall 檢驗(yàn):數(shù)據(jù)趨勢(shì)與突變分析的有力工具? ? ? 在數(shù)據(jù)分析的廣袤領(lǐng)域中,準(zhǔn)確捕捉數(shù)據(jù)的趨勢(shì)變化以及識(shí)別 ...
2025-07-08備戰(zhàn) CDA 數(shù)據(jù)分析師考試:需要多久?如何規(guī)劃? CDA(Certified Data Analyst)數(shù)據(jù)分析師認(rèn)證作為國(guó)內(nèi)權(quán)威的數(shù)據(jù)分析能力認(rèn)證 ...
2025-07-08LSTM 輸出不確定的成因、影響與應(yīng)對(duì)策略? 長(zhǎng)短期記憶網(wǎng)絡(luò)(LSTM)作為循環(huán)神經(jīng)網(wǎng)絡(luò)(RNN)的一種變體,憑借獨(dú)特的門控機(jī)制,在 ...
2025-07-07統(tǒng)計(jì)學(xué)方法在市場(chǎng)調(diào)研數(shù)據(jù)中的深度應(yīng)用? 市場(chǎng)調(diào)研是企業(yè)洞察市場(chǎng)動(dòng)態(tài)、了解消費(fèi)者需求的重要途徑,而統(tǒng)計(jì)學(xué)方法則是市場(chǎng)調(diào)研數(shù) ...
2025-07-07CDA數(shù)據(jù)分析師證書考試全攻略? 在數(shù)字化浪潮席卷全球的當(dāng)下,數(shù)據(jù)已成為企業(yè)決策、行業(yè)發(fā)展的核心驅(qū)動(dòng)力,數(shù)據(jù)分析師也因此成為 ...
2025-07-07剖析 CDA 數(shù)據(jù)分析師考試題型:解鎖高效備考與答題策略? CDA(Certified Data Analyst)數(shù)據(jù)分析師考試作為衡量數(shù)據(jù)專業(yè)能力的 ...
2025-07-04SQL Server 字符串截取轉(zhuǎn)日期:解鎖數(shù)據(jù)處理的關(guān)鍵技能? 在數(shù)據(jù)處理與分析工作中,數(shù)據(jù)格式的規(guī)范性是保證后續(xù)分析準(zhǔn)確性的基礎(chǔ) ...
2025-07-04CDA 數(shù)據(jù)分析師視角:從數(shù)據(jù)迷霧中探尋商業(yè)真相? 在數(shù)字化浪潮席卷全球的今天,數(shù)據(jù)已成為企業(yè)決策的核心驅(qū)動(dòng)力,CDA(Certifie ...
2025-07-04CDA 數(shù)據(jù)分析師:開啟數(shù)據(jù)職業(yè)發(fā)展新征程? ? 在數(shù)據(jù)成為核心生產(chǎn)要素的今天,數(shù)據(jù)分析師的職業(yè)價(jià)值愈發(fā)凸顯。CDA(Certified D ...
2025-07-03