遍历目标文件夹下所有文件
来源:互联网 发布:网络制式有哪些 编辑:程序博客网 时间:2024/04/27 10:47
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
#全局变量设置歌曲预定格式
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-遍历目标文件夹下所有文件
- 遍历目标文件夹下所有文件
- 遍历文件夹下所有文件
- 遍历文件夹下所有文件
- 遍历文件夹下所有文件
- 遍历文件夹下所有文件
- 遍历文件夹下所有文件和文件夹
- C++删除目标文件夹下所有文件
- php遍历目标文件夹下的文件和子文件夹
- VC下遍历文件夹中的所有文件
- oracle遍历文件夹下所有文件
- 遍历文件夹下的所有文件
- 遍历文件夹下的所有文件
- (转)C++遍历文件夹下所有文件
- vc遍历文件夹下的所有文件
- c++遍历文件夹下所有文件
- node遍历文件夹下所有文件
- 遍历文件夹下的所有文件
- Python操作SMTP和POP3
- 征集佳句-精妙SQL语句收集
- Delphi自写组件:可设置颜色的按钮
- 使用HtmlAgilityPack更好的HTML分析和验证
- 按钮3D效果
- 遍历目标文件夹下所有文件
- linux关机和重启命令
- 按值传递还是按引用传递
- 没牛逼以前还是老老实实的吧。
- 大象--Thinking in UML早知道 -- 002--面向过程方法与面向对象方法
- 使用OpenFileDialog选择文件
- 特殊SQL语句及优化原则
- 大象--Thinking in UML早知道 -- 003--基本建模方法
- ASC码对照表