您的位置:首页 > 编程语言 > Python开发

Python-遍历目标文件夹下所有文件

2007-07-12 16:05 591 查看
 






# -*- coding: cp936 -*-






#文件:BASE.py


#用途:遍历目的文件夹所有文件,并根据过滤标志返回属于过滤条件返回的文件绝对地址


#作者:刘华飞


#版本: 0.01


#时间:2007年5月11日


#授权:本程序可以免费使用,转载修改必修附上原作者信息










import os




#全局变量设置歌曲预定格式


Const_Song_Format=["mp3","wma","ogg"]






class BASE:


        #类变量,设置文件列表


        fileList=[""]


        #类变量,设置文件计算


        counter=0


        def __init__(self):


                pass


        def RecusWalkDir(self,dir,filtrate=0):


                """本方法递归遍历目的文件夹中所有文件,获取指定格式的文件绝对地址,利用类变量fileList存储地址"""


                global Const_Song_Format


                for s in os.listdir(dir):


                        newDir=dir+"/"+s


                        if os.path.isdir(newDir):


                                self.RecusWalkDir(newDir)


                        else:


                                if os.path.isfile(newDir):


                                        if filtrate:


                                                if newDir and (self.GetFileFormat(newDir) in Const_Song_Format):


                                                        self.__class__.fileList.append(newDir)


                                                        self.__class__.counter+=1


                                        else:


                                                self.__class__.fileList.append(newDir)


                                                self.__class__.counter+=1










        def CycWalkDir(self,dir,filtrate=0):


                """本方法循环遍历文件夹中所有文件,获取指定格式的文件绝对地址,返回歌曲列表fileList"""


                global Const_Song_Format


                fileList=[""]


                for s in os.listdir(dir):


                        newDir=dir+"/"+s


                        if os.path.isfile(newDir):


                                if filtrate:


                                        if newDir and (self.GetFileFormat(newDir) in Const_Song_Format):


                                                fileList.append(newDir)


                                else:


                                        fileList.append(newDir)


                        else:


                                newDir=dir+"/"+s


                        while os.path.isdir(newDir):


                                        for s in os.listdir(dir):


                                                newDir=dir+"/"+s


                                                if os.path.isfile(newDir):


                                                        if filtrate:


                                                                if newDir and (self.GetFileFormat(newDir) in Const_Song_Format):


                                                                        fileList.append(newDir)


                                                        else:


                                                                fileList.append(newDir)


                                                else:


                                                        newDir=dir+"/"+s


                return fileList






        def GetFileFormat(self,fileName):


                """返回文件格式"""


                if fileName:


                        BaseName=os.path.basename(fileName)


                        str=BaseName.split(".")


                        return str[-1]


                else:


                        return fileName




if __name__=="__main__":


        b=BASE()


        b.RecusWalkDir(dir="E:/音乐无限")


        print (b.counter)


        for k in  b.fileList:


                        print k

 

今天在CSDN上看到一个新的Python网站,在其中发现的。

网址:http://www.pythonbbs.cn/thread-2153-1-1.html
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息