1. 程式人生 > >Python 讀取指定目錄下的所有子目錄及所有檔案

Python 讀取指定目錄下的所有子目錄及所有檔案

掃描指定目錄下的檔案,或者匹配指定字尾和字首的檔案。

如果要掃描指定目錄下的檔案,包括子目錄,呼叫scan_files("/export/home/test/")

如果要掃描指定目錄下的特定字尾的檔案(比如jar包),包括子目錄,呼叫scan_files("/export/home/test/", postfix=".jar")

如果要掃描指定目錄下的特定字首的檔案(比如test_xxx.py),包括子目錄,呼叫scan_files("/export/home/test/", prefix="test_")

'''
Created on Sep 19, 2014

@author: liu.chunming
'''
#!/usr/bin/env python  
#coding=utf-8  

import os

class ScanFile(object): 
    def __init__(self,directory,prefix=None,postfix=None):
        self.directory=directory
        self.prefix=prefix
        self.postfix=postfix
        
    def scan_files(self):  
        files_list=[]  
          
        for dirpath,dirnames,filenames in os.walk(self.directory): 
            '''
            dirpath is a string, the path to the directory.  
            dirnames is a list of the names of the subdirectories in dirpath (excluding '.' and '..').
            filenames is a list of the names of the non-directory files in dirpath.
            '''
            for special_file in filenames:  
                if self.postfix:  
                    special_file.endswith(self.postfix)  
                    files_list.append(os.path.join(dirpath,special_file))  
                elif self.prefix:  
                    special_file.startswith(self.prefix)
                    files_list.append(os.path.join(dirpath,special_file))  
                else:  
                    files_list.append(os.path.join(dirpath,special_file))  
                                
        return files_list  
    
    def scan_subdir(self):
        subdir_list=[]
        for dirpath,dirnames,files in os.walk(self.directory):
            subdir_list.append(dirpath)
        return subdir_list

if __name__=="__main__":
    dir=r"C:\os_path"
    scan=ScanFile(dir)
    subdirs=scan.scan_subdir()
    files=scan.scan_files()
    
    print "The subdirs scaned are:"
    for subdir in subdirs:
        print subdir
    
    print "The files scaned are:"
    for file in files:
        print file

執行結果為:

The subdirs scaned are:
C:\os_path
C:\os_path\..New folder
C:\os_path\Camera
The files scaned are:
C:\os_path\..Copy.jpg
C:\os_path\20130930_112514.jpg
C:\os_path\20130930_112519.jpg
C:\os_path\20130930_112547.jpg
C:\os_path\20131006_182643.jpg
C:\os_path\20131006_182825.jpg
C:\os_path\20131006_182836.jpg
C:\os_path\20131006_183124.jpg
C:\os_path\20131006_183128.jpg
C:\os_path\20131006_183139.jpg
C:\os_path\20131006_183142.jpg
C:\os_path\20131022_133724.jpg
C:\os_path\20131106_150907.jpg
C:\os_path\20131106_184849.jpg
C:\os_path\20131106_184856.jpg
C:\os_path\20131106_184857.jpg
C:\os_path\20131106_185109.jpg
C:\os_path\20131106_185112.jpg
C:\os_path\20131106_185113.jpg
C:\os_path\20131106_185115.jpg
C:\os_path\20131106_190934.jpg
C:\os_path\20131106_190939.jpg
C:\os_path\20131106_190953.jpg
C:\os_path\20131106_191434.mp4
C:\os_path\20131107_195616.jpg
C:\os_path\20131107_195627.jpg
C:\os_path\20131107_195641.jpg
C:\os_path\20131107_195643.jpg
C:\os_path\20131109_170815.jpg
C:\os_path\20131109_170821.jpg
C:\os_path\20131112_074715.jpg
C:\os_path\20131112_082321.jpg
C:\os_path\20131112_202438.jpg
C:\os_path\20131112_202441.jpg
C:\os_path\20131112_202444.jpg
C:\os_path\20131112_202445.jpg
C:\os_path\20131201_115344.jpg
C:\os_path\20131201_205319.jpg
C:\os_path\20131201_205322.jpg
C:\os_path\20140107_205105.jpg
C:\os_path\20140107_205141.jpg
C:\os_path\20140117_162741.jpg
C:\os_path\20140117_162744.jpg
C:\os_path\20140117_162858.jpg
C:\os_path\20140117_162906.jpg
C:\os_path\20140117_184511.jpg
C:\os_path\20140117_184559.jpg
C:\os_path\20140124_151807.jpg
C:\os_path\20140124_151832.jpg
C:\os_path\20140128_140001.jpg
C:\os_path\20140128_140016.jpg
C:\os_path\20140128_140721.jpg
C:\os_path\20140128_140736.jpg
C:\os_path\20140128_190857.jpg
C:\os_path\20140129_073525.jpg
C:\os_path\20140130_110123.jpg
C:\os_path\20140130_110129.jpg
C:\os_path\20140205_212217.jpg
C:\os_path\20140205_212230.jpg
C:\os_path\20140205_212300.jpg
C:\os_path\20140205_212307.jpg
C:\os_path\20140205_212310.jpg
C:\os_path\20140205_212316.jpg
C:\os_path\20140205_212345.jpg
C:\os_path\20140205_212356.jpg
C:\os_path\20140205_212357.jpg
C:\os_path\20140205_212400.jpg
C:\os_path\20140205_212502.jpg
C:\os_path\20140207_100552.jpg
C:\os_path\20140207_100612.jpg
C:\os_path\20140302_145458.jpg
C:\os_path\20140302_145521.jpg
C:\os_path\Camera\..New Text Document.txt
C:\os_path\Camera\20130930_112514.jpg
C:\os_path\Camera\20130930_112519.jpg
C:\os_path\Camera\20130930_112547.jpg
C:\os_path\Camera\20131006_182643.jpg
C:\os_path\Camera\20131006_182825.jpg
C:\os_path\Camera\20131006_182836.jpg
C:\os_path\Camera\20131006_183124.jpg
C:\os_path\Camera\20131006_183128.jpg
C:\os_path\Camera\20131006_183139.jpg
C:\os_path\Camera\20131006_183142.jpg