最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

使用 Python 實(shí)現(xiàn)文件遞歸遍歷的三種方式

 更新時(shí)間:2018年07月18日 10:20:55   作者:sylan215  
這篇文章主要介紹了使用 Python 實(shí)現(xiàn)文件遞歸遍歷的三種方式,非常不錯(cuò),具有一定的參考借鑒價(jià)值,需要的朋友參考下吧

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

先發(fā)出來看看:

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 個(gè)函數(shù),并且每個(gè)函數(shù)都用了一次 listdir,只是一次用來過濾文件,一次用來過濾文件夾,如果只是從功能實(shí)現(xiàn)上看,一點(diǎ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

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

有木有更好的方式呢?網(wǎng)上一搜一大把,原來有一個(gè)現(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)

只是從代碼實(shí)現(xiàn)上看,方案二是最優(yōu)雅簡潔的了,但是再翻看 os.walk() 實(shí)現(xiàn)的源碼就會(huì)發(fā)現(xiàn),其實(shí)它內(nèi)部還是調(diào)用的 listdir 完成具體的功能實(shí)現(xiàn),只是它對(duì)輸出結(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 在輸出時(shí)的不同點(diǎn),主要就是 listdir 默認(rèn)是按照文件和文件夾存放的字母順序進(jìn)行輸出,而 walk 則是先輸出頂級(jí)文件夾,然后是頂級(jí)文件,再輸出第二級(jí)文件夾,以及第二級(jí)文件,以此類推,具體大家可以把上面腳本拷貝后自行驗(yàn)證。

總結(jié)

以上所述是小編給大家介紹的使用 Python 實(shí)現(xiàn)文件遞歸遍歷的三種方式,希望對(duì)大家有所幫助,如果大家有任何疑問請(qǐng)給我留言,小編會(huì)及時(shí)回復(fù)大家的。在此也非常感謝大家對(duì)腳本之家網(wǎng)站的支持!

相關(guān)文章

最新評(píng)論

大姚县| 贡觉县| 确山县| 青冈县| 紫金县| 盐源县| 清水河县| 高州市| 天津市| 平定县| 阿拉善左旗| 洛扎县| 惠东县| 红安县| 栖霞市| 石狮市| 高密市| 曲周县| 陇川县| 天全县| 沈阳市| 冕宁县| 鞍山市| 海口市| 林西县| 徐闻县| 苗栗市| 望江县| 佛学| 千阳县| 城口县| 巨野县| 海林市| 延长县| 扎兰屯市| 丹江口市| 湾仔区| 赣州市| 翁源县| 冕宁县| 乌鲁木齐县|