Python遍历文件夹枚举所有文件类型

时间:2021-04-14 12:26:34
>>> import os
>>> def enumfiles(path, dest):
	files = os.listdir(path)
	for f in files:
		subpath = path + '/' + f
		if (os.path.isfile(subpath)):
			dest.append(subpath)
		elif (os.path.isdir(subpath)):
			if (f[0] == '.'):
				pass
			else:
				enumfiles(subpath, dest)

				
>>> files = []
>>> path = "D:\cocos2d-x-2.2.3"
>>> enumfiles(path, files)
>>> exts = {}
>>> for f in files:
	l = len(f)
	p = f.rfind('.')
	s = f.rfind('/')
	if (l>0 and p!=-1 and p>s):
		ext = f[p:l]
		exts[ext] = 0

>>> exts.keys()

项目中,有时候需要在SVN中忽略掉某些文件类型,上面的脚本可以轻松做到,使用了os库,递归和字典的键值唯一性。