아미(아름다운미소)

Python directory search and counting by specific extensions 본문

랭귀지/PYTHON

Python directory search and counting by specific extensions

유키공 2018. 9. 8. 11:00

python 디렉토리 전체탐색후 특정확장자별로 카운트하기

# -*- coding: utf-8 -*- 
'''
Created on 2018. 9. 5.

@author: bhm
'''
import os   
from datetime import datetime
class CGetAllFiles :
    def __init__( self ) :
        pass
 
    def getFiles( self , recdir ):
        x = 0
        y = 0
        for pack in os.walk(recdir):
            for f in pack[2]:     
                if os.path.splitext(f)[1].lower() =='.wav':
                    x += 1
                if os.path.splitext(f)[1].lower() =='.mp3':
                    y += 1    
        print ( ("Dir: %s , there are %s of mp3 files") % ( recdir, str(y) ) )            
        print ( ("Dir: %s , there are %s of wav files") % ( recdir, str(x) ) )
        
if __name__ == '__main__':
    dt = datetime.now()
    dir_Ym  = dt.strftime("%Y%m")
    dir_Ymd = dt.strftime("%Y%m%d")
    org_foldername = u"D:/RecData/한글디렉토리/" + dir_Ym + "/" + dir_Ymd
    f = CGetAllFiles()
    f.getFiles(org_foldername)
결과 :
Dir: D:/RecData/한글디렉토리/201809/20180905/ , there are 3 of mp3 files
Dir: D:/RecData/한글디렉토리/201809/20180905 , there are 2 of wav files
Comments