python 遍历文件夹目录,三个函数

#coding=utf-8
import sys,os,os.path
import pdb

#只获得第一层子目录,过滤非目录文件

def getsubdocs(path=None):
doclist = []
if(os.path.isdir(path)):
for item in os.listdir(path):
if item != '.svn':
if os.path.isdir(path+os.sep+item):
doclist.append(item)
return doclist
 
#获得所有层级子目录,过滤非目录文件

def getallsubdocs(path=None):
#pdb.set_trace()
doclist = []
if(os.path.isdir(path)):
if len(os.listdir(path)) > 0:
for item in os.listdir(path):
if item != '.svn':
doclist.extend(getallsubdocs(path+os.sep+item))
else:
doclist.append(path)
return doclist
 
#获取所有子目录中,不包括svn相关的目录
 
def getalldocs(path=None):
doclist = []
for root,dirs,files in os.walk(path):
if root.find('.svn') < 0:
doclist.append(root)
return doclist
if __name__=='__main__':
path = raw_input('Enter root path(/*/*/....): ')
doclist = getallsubdocs(path)
print doclist

你可能感兴趣的:(python,函数,职场,休闲,遍历文件夹目录)