欧美一区二区三区老妇人-欧美做爰猛烈大尺度电-99久久夜色精品国产亚洲a-亚洲福利视频一区二区

怎么在Python中利用遞歸遍歷文件-創(chuàng)新互聯(lián)

今天就跟大家聊聊有關(guān)怎么在 Python中利用遞歸遍歷文件,可能很多人都不太了解,為了讓大家更加了解,小編給大家總結(jié)了以下內(nèi)容,希望大家根據(jù)這篇文章可以有所收獲。

10年積累的成都網(wǎng)站制作、成都網(wǎng)站設(shè)計經(jīng)驗,可以快速應(yīng)對客戶對網(wǎng)站的新想法和需求。提供各種問題對應(yīng)的解決方案。讓選擇我們的客戶得到更好、更有力的網(wǎng)絡(luò)服務(wù)。我雖然不認識你,你也不認識我。但先網(wǎng)站設(shè)計后付款的網(wǎng)站建設(shè)流程,更有硯山免費網(wǎng)站建設(shè)讓你可以放心的選擇與我們合作。
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):
    getallfiles(dir)
    dirlist = os.listdir(dir)
    for dirret in dirlist:
      fullname = dir + "\\" + dirret
      if os.path.isdir(fullname):
        getalldirfiles(fullname, basedir)

我是用了 2 個函數(shù),并且每個函數(shù)都用了一次 listdir,只是一次用來過濾文件,一次用來過濾文件夾,如果只是從功能實現(xiàn)上看,一點問題沒有,但是這…太不優(yōu)雅了吧。

開始著手優(yōu)化,方案一:

def getallfiles(dir):
"""使用listdir循環(huán)遍歷"""
  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

從上圖可以看到,我把兩個函數(shù)合并成了一個,只調(diào)用了一次 listdir,把文件和文件夾用 if~else~ 進行了分支處理,當(dāng)然,自我調(diào)用的循環(huán)還是存在。

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

方案二:

def getallfilesofwalk(dir):
"""使用listdir循環(huán)遍歷"""
  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)

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

附上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 則是先輸出頂級文件夾,然后是頂級文件,再輸出第二級文件夾,以及第二級文件,以此類推,具體大家可以把上面腳本拷貝后自行驗證。

看完上述內(nèi)容,你們對怎么在 Python中利用遞歸遍歷文件有進一步的了解嗎?如果還想了解更多知識或者相關(guān)內(nèi)容,請關(guān)注創(chuàng)新互聯(lián)行業(yè)資訊頻道,感謝大家的支持。

當(dāng)前題目:怎么在Python中利用遞歸遍歷文件-創(chuàng)新互聯(lián)
文章起源:http://chinadenli.net/article2/edcoc.html

成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供自適應(yīng)網(wǎng)站網(wǎng)站改版品牌網(wǎng)站設(shè)計品牌網(wǎng)站建設(shè)做網(wǎng)站服務(wù)器托管

廣告

聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時需注明來源: 創(chuàng)新互聯(lián)

營銷型網(wǎng)站建設(shè)