1. 程式人生 > >Python os.walk檔案遍歷

Python os.walk檔案遍歷

os.walk(top, topdown=True, onerror=None, followlinks=False)
可以得到一個三元tupple(dirpath, dirnames, filenames),
第一個為起始路徑,第二個為起始路徑下的資料夾,第三個是起始路徑下的檔案。
dirpath 是一個string,代表目錄的路徑,
dirnames 是一個list,包含了dirpath下所有子目錄的名字。
filenames 是一個list,包含了非目錄檔案的名字。
這些名字不包含路徑資訊,如果需要得到全路徑,需要使用os.path.join(dirpath, name).

通過for迴圈自動完成遞迴列舉

import os
for i in os.walk('c:'+os.sep+'ant'):
    print i

輸出:

(‘c:\ant’, [‘bin’, ‘docs’, ‘etc’, ‘lib’, ‘Project’], [‘fetch.xml’, ‘get-m2.xml’, ‘INSTALL’, ‘KEYS’, ‘LICENSE’, ‘NOTICE’, ‘README’, ‘WHATSNEW’])
(‘c:\ant\bin’, [], [‘ant’, ‘ant.bat’, ‘ant.cmd’, ‘antenv.cmd’, ‘antRun’, ‘antRun.bat’, ‘antRun.pl’, ‘complete-ant-cmd.pl’, ‘envset.cmd’, ‘lcp.bat’, ‘runant.pl’, ‘runant.py’, ‘runrc.cmd’])
(‘c:\ant\docs’, [‘ant2’, ‘antlibs’, ‘images’, ‘manual’, ‘projects’, ‘slides’, ‘webtest’], [‘antnews.html’, ‘ant_in_anger.html’, ‘ant_task_guidelines.html’, ‘appendix_e.pdf’, ‘breadcrumbs.js’, ‘bugs.html’, ‘bylaws.html’, ‘contributors.html’, ‘external.html’, ‘faq.html’, ‘favicon.ico’, ‘index.html’, ‘legal.html’, ‘LICENSE’, ‘license.html’, ‘mail.html’, ‘mission.html’, ‘nightlies.html’, ‘page.css’, ‘problems.html’, ‘projects.html’, ‘resources.html’, ‘svn.html’])
(‘c:\ant\docs\ant2’, [], [‘actionlist.html’, ‘features.html’, ‘FunctionalRequirements.html’, ‘original-specification.html’, ‘requested-features.html’, ‘requested-features.txt’, ‘VFS.txt’])““““““““““““““

遍歷資料夾並刪除特定格式檔案的示例

#!/usr/bin/python
# -*- coding: utf-8 -*-
import os
def del_files(path):
    for root , dirs, files in os.walk(path):
        for name in files:
            if name.endswith(".tmp"):
                os.remove(os.path.join(root, name))
  print ("Delete File: " + os.path.join(root, name))
# test
if __name__ == "__main__": path = '/tmp' del_files(path)

通過for迴圈即可完成目錄的遞迴

#!/usr/bin/python
# -*- coding: gbk -*-

# os.walk()的使用  
import os  

# 列舉dirPath目錄下的所有檔案  

def main():  
#begin  
    fileDir = "F:" + os.sep + "aaa"     # 查詢F:\aaa 目錄下    
    for root, dirs, files in os.walk(fileDir):  
    #begin  
        for dir in dirs:  
        #begin  
            print(os.path.join(root, dir))  
        #end  
        for file in files:  
        #begin  
            print(os.path.join(root, file))  
        #end  
    #end  
    os.system("pause")  
#end  

if __name__ == '__main__':  
#begin  
    main()  
#end