如何優雅的使用 Python 實現文件遞歸遍歷


今天有個腳本需要遍歷獲取某指定文件夾下面的所有文件,我記得很早前也實現過文件遍歷和目錄遍歷的功能,於是找來看一看,嘿,不看不知道,看了嚇一跳,原來之前我竟然用了這么搓的實現。

先發出來看看:

def getallfiles(dir):
"""遍歷獲取指定文件夾下面所有文件"""   
    if os.path.isdir(dir):
        filelist = os.listdir(dir)
        for ret in filelist:
            filename = dir + "\\" + ret
            if os.path.isfile(filename):
                print filename
                
def getalldirfiles(dir, basedir):
"""遍歷獲取所有子文件夾下面所有文件"""   
    if os.path.isdir(dir):
  muiop[]
sdrtyuiop[]
      getallfiles(dir)
        dirlist = os.listdir(dir)
        for dirret in dirlist:
            fullname = dir + "\\" + dirret
            if os.path.isdir(fullname):
                getalldirfiles(fullname, basedir)

我是用了 2 個函數,並且每個函數都用了一次 listdir,只是一次用來過濾文件,一次用來過濾文件夾,如果只是從功能實現上看,一點問題沒有,但是這…太不優雅了吧。

開始着手優化,方案一:

def getallfiles(dir):
"""使用listdir循環遍歷"""   
    if not os.path.isdir(dir):
        print dir
        return   
    dirlist = os.listdir(dir)
    for dirret in dirlist:
        fullname = dir + "\\" + dirret
        if os.path.isdir(fullname):
            getallfiles(fullname)
        else:
            print fullname

從上圖可以看到,我把兩個函數合並成了一個,只調用了一次 listdir,把文件和文件夾用 ifelse 進行了分支處理,當然,自我調用的循環還是存在。

有木有更好的方式呢?網上一搜一大把,原來有一個現成的 os.walk() 函數可以用來處理文件(夾)的遍歷,這樣優化下就更簡單了。

方案二:

def getallfilesofwalk(dir):
"""使用listdir循環遍歷"""   
    if not os.path.isdir(dir):
        print dir
        return   
    dirlist = os.walk(dir)
    for root, dirs, files in dirlist:
        for file in files:
            print os.path.join(root, file)

只是從代碼實現上看,方案二是最優雅簡潔的了,但是再翻看 os.walk() 實現的源碼就會發現,其實它內部還是調用的 listdir 完成具體的功能實現,只是它對輸出結果做了下額外的處理而已。

附上os.walk()的源碼:

from os.path import join, isdir, islink

# We may not have read permission for top, in which case we can't
# get a list of the files the directory contains.  os.path.walk
# always suppressed the exception then, rather than blow up for a
# minor reason when (say) a thousand readable directories are still
# left to visit.  That logic is copied here.

try:
    # Note that listdir and error are globals in this module due   
    # to earlier import-*.   
    names = listdir(top)
except error, err:
    if onerror is not None:
       onerror(err)
    return
    
dirs, nondirs = [], []
for name in names:
    if isdir(join(top, name)):
        dirs.append(name)
    else:
        nondirs.append(name)
        
if topdown:
    yield top, dirs, nondirs
for name in dirs:
    path = join(top, name)
    if followlinks or not islink(path):
        for x in walk(path, topdown, onerror, followlinks):
            yield x
if not topdown:
    yield top, dirs, nondirs

 

至於 listdir 和 walk 在輸出時的不同點,主要就是listdir 默認是按照文件和文件夾存放的字母順序進行輸出,而 walk 則是先輸出頂級文件夾,然后是頂級文件,再輸出第二級文件夾,以及第二級文件,以此類推,具體大家可以把上面腳本拷貝后自行驗證。

結語:

跟大家推薦一個學習資料分享群:706315665,里面大牛已經為我們整理好了許多的學習資料,有自動化,接口,性能等等的學習資料!人生是一個逆水行舟的過程,不進則退,咱們一起加油吧!


免責聲明!

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



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