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
相关文章推荐
- 遍历文件夹下的所有文件(Python&C++)
- python 遍历文件夹中所有文件
- Python遍历某目录下的所有文件夹与文件路径
- python - 遍历某个文件夹下的所有子文件夹和文件名称
- python下遍历文件夹中所有文件
- 【Python】遍历文件夹中所有文件
- Python遍历文件夹下所有文件及目录
- 如何用python遍历文件夹下的所有excel文件
- python练习(遍历所有文件夹及文件)
- python 遍历所有文件文件夹,并且搜索其中的字符串
- Python:遍历一个目录下所有的文件及文件夹,然后计算每个文件的字符和line的小程序
- 遍历目标文件夹下所有文件
- Python 如何遍历文件夹以及子文件夹下的所有文件
- Python遍历文件夹下所有文件中数据并写入Excel
- python 遍历文件夹下面所有的文件
- 【Python】遍历文件夹,得到所有文件
- 【Python】遍历文件夹中所有文件
- windows下用c++和python遍历目录下的文件夹并删除所有文件名为xx的文件
- python 遍历指定文件夹下的所有文件内容
- python 遍历一个文件夹下所有文件名称