读书人

DiskWalk 遍历目录树工具种

发布时间: 2012-10-07 17:28:51 作者: rapoo

DiskWalk 遍历目录树工具类

import osclass DiskWalk(object):    '''    API for getting directory walking collections    '''    def __init__(self, path):        if path is None or len(path) == 0:            print 'Error: Please enter a valid path!'            return False        self.path = path    def enumeratePaths(self):        '''        Returns the path to all the files in a directory as a list        '''        path_collection = []        for dirpath, dirnames, filenames in os.walk(self.path):            for file in filenames:                fullpath = os.path.join(dirpath, file)                path_collection.append(fullpath)        return path_collection    def enumerateFiles(self):        '''        Returns all the files in a directory as a list        '''        path_collection = []        for dirpath, dirnames, filenames in os.walk(self.path):            for file in filenames:                path_collection.append(file)        return path_collection    def enumerateDir(self):        '''        Returns all the directories in a directory as a list        '''        path_collection = []        for dirpath, dirnames, filenames in os.walk(self.path):            for dir in dirnames:                path_collection.append(dir)        return path_collection
?

读书人网 >编程

热点推荐