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

本文介绍了一个Python程序,该程序能够遍历指定文件夹及其子文件夹,收集所有符合预设格式(如mp3、wma等)的音乐文件路径。程序提供两种遍历方式:递归和循环,并通过类变量存储文件列表。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

 


# -*- 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

 

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

网址:http://www.pythonbbs.cn/thread-2153-1-1.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值